I have a std::vector<std::string> m_vPaths;
I iterate over this vector and call ::DeleteFile(strPath)
as I go. If I successfully delete the file, I remove it from the vector.
My question is: can I get around having to use two vectors? Is there a different data structure that might be better suited for what I need to do?
Example
Using iterators almost does what I want, but the problem is that 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've used erase,
// but I want to continue deleting 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?
In case it is unclear, m_vPaths
is declared like this (in my class):
std::vector<std::string> m_vPaths;