全,
std::vector<string>::iterator it;
string orig;
bool found = false;
for( it = vec.begin(); it < vec.end() && !found; it++ )
{
if( ... )
{
found = true;
orig = (*it);
}
}在我退出循环之后,即使我有found = true,迭代器也会变得无效。
如何保留迭代器?我需要它以后再处理。
MSVC 2017,Windows 8.1
蒂娅!
发布于 2022-03-28 14:00:44
在找到it的情况下,您可以减少它,以撤消您不想要的最终it++。
if (found) it--;或者您可以使用std::find_if,其中...使用value而不是*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;https://stackoverflow.com/questions/71648632
复制相似问题