'Iterator becomes nvalid
ALL,
std::vector<string>::iterator it;
string orig;
bool found = false;
for( it = vec.begin(); it < vec.end() && !found; it++ )
{
if( ... )
{
found = true;
orig = (*it);
}
}
After I get out of the loop the iterator become invalid even if I have found = true.
How do I keep the iterator? I need it for later processing..
MSVC 2017, Windows 8.1
TIA!!!
Solution 1:[1]
You could decrement it in the case you found it, to undo the final it++ that you don't want.
if (found) it--;
Or you could use std::find_if, where ... uses value instead of *it.
auto it = std::find_if(vec.begin(), vec.end(), [](std::string & value) ( return value.find("abc"); });
auto found = it != vec.end();
auto orig = *it;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
