久久久久久久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 ()?環形?)
主站蜘蛛池模板: 成人午夜在线 | 婷婷国产一区二区三区 | 久久国产精品久久国产精品 | 久久久欧洲 | 国产一区二区三区视频免费观看 | 国产欧美日韩精品一区二区三区 | 视频三区| 69av网| 在线欧美亚洲 | 成人a视频 | 成人午夜av| 亚洲视频一区二区三区四区 | 国产精品久久久久一区二区三区 | 精品二三区 | 一区二区电影网 | 天天综合网天天综合色 | 欧美一级免费观看 | 国产精品婷婷 | 国产亚洲一区二区三区 | 亚洲视频欧美视频 | www.四虎.com| 久久久成人免费一区二区 | 午夜视频一区二区 | 综合久久亚洲 | 欧美性一区二区三区 | 久久大 | 国产精品不卡一区 | 日本黄色免费片 | 欧美13videosex性极品 | 九九激情视频 | 一区二区三区四区在线视频 | 欧美在线观看一区 | 中文字幕在线二区 | 亚洲一区影院 | 一级毛片免费完整视频 | 精品久久视频 | 91久久久久久久久久久久久 | 乱码av午夜噜噜噜噜动漫 | 成人免费网站 | 精品一区二区三区在线观看国产 | 日韩欧美在线免费观看 |