久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

迭代向量,邊走邊刪除某些項目

iterate vector, remove certain items as I go(迭代向量,邊走邊刪除某些項目)
本文介紹了迭代向量,邊走邊刪除某些項目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 std::vector m_vPaths;我將迭代這個向量并隨時調用 ::DeleteFile(strPath) .如果我成功刪除了文件,我會將其從向量中刪除.我的問題是我可以避免使用兩個向量嗎?是否有不同的數據結構可能更適合我需要做的事情?

I have a std::vector m_vPaths; I will iterate this vector and call ::DeleteFile(strPath) as I go. If I successfully delete the file, I will remove it from the vector. My question is can I get around having to use two vectors? Is there different data structure that might be better suited for what I need to do?

示例:使用迭代器幾乎可以滿足我的要求,但問題是一旦使用迭代器擦除,所有迭代器都將無效.

example: using iterators almost does what I want, but problem is once you erase using an iterator, all iterators become invalid.

 std::vector<std::string> iter = m_vPaths.begin();
    for( ; iter != m_vPaths.end(); iter++) {
        std::string strPath = *iter;
        if(::DeleteFile(strPath.c_str())) {
            m_vPaths.erase(iter);   
                //Now my interators are invalid because I used erase,
                //but I want to continue deleteing the files remaining in my vector.    
        }
    }

我可以使用兩個向量并且不再有問題,但是有沒有更好、更有效的方法來做我想做的事情?

I can use two vectors and I will no longer have a problem, but is there a better, more efficient method of doing what I'm trying to do?

順便說一句,如果不清楚,m_vPaths 是這樣聲明的(在我的班級中):

btw, incase it is unclear, m_vPaths is declared like this (in my class):

std::vector<std::string> m_vPaths;

推薦答案

查看 std::remove_if:

#include <algorithm> // for remove_if
#include <functional> // for unary_function

struct delete_file : public std::unary_function<const std::string&, bool> 
{
    bool operator()(const std::string& strPath) const
    {
        return ::DeleteFile(strPath.c_str());
    }
}

m_vPaths.erase(std::remove_if(m_vPaths.begin(), m_vPaths.end(), delete_file()),
                m_vPaths.end());

使用 std::list停止無效迭代器問題,盡管您失去了隨機訪問.(和緩存性能,一般)

Use a std::list to stop the invalid iterators problem, though you lose random access. (And cache performance, in general)

為了記錄,您實現代碼的方式是:

For the record, the way you would implement your code would be:

typedef std::vector<std::string> string_vector;
typedef std::vector<std::string>::iterator string_vector_iterator;

string_vector_iterator iter = m_vPaths.begin();
while (iter != m_vPaths.end())
{
    if(::DeleteFile(iter->c_str()))
    {
        // erase returns the new iterator
        iter = m_vPaths.erase(iter);
    }
    else
    {
        ++iter;
    }
}

但是你應該使用 std::remove_if(重新發明輪子不好).

But you should use std::remove_if (reinventing the wheel is bad).

這篇關于迭代向量,邊走邊刪除某些項目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 日韩伦理一区二区 | 亚洲性网 | 亚洲精品乱码久久久久久按摩观 | 久草福利 | 伊人天堂网 | 精品久久久久久亚洲综合网站 | 一区二区三区视频免费观看 | 一级毛片视频 | 日本天堂一区二区 | 91精品国产综合久久久久蜜臀 | 人妖av| 精品综合 | 91色视频在线观看 | 国产精品视频免费观看 | 欧美一区二区三区在线看 | 日韩黄色av| 成人激情视频在线观看 | 国产精品mv在线观看 | 久久大陆 | 午夜精品久久久久久不卡欧美一级 | 日韩高清成人 | 久草免费在线视频 | 国产在线永久免费 | 天天天操天天天干 | 日韩在线精品视频 | 精品免费国产视频 | 欧美精品一区二区在线观看 | 国产一区二区三区在线看 | 怡红院免费的全部视频 | 欧美精品99 | 九色在线观看 | 一区二区在线 | 免费在线观看av网址 | 日日夜夜精品 | 欧美一级特黄aaa大片在线观看 | 天天干天天干 | www.五月婷婷.com| 久久国产日本 | 久久一区二区三区四区五区 | 国产一二三区精品视频 | 97久久超碰|