问题遇到的现象和发生背景
c++ find_if()函数查找自定义类型时返回的结束迭代器在for循环中不能结束循环
用代码块功能插入代码,请勿粘贴截图
// 对于基本数据类型没有问题
for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
{
it = find_if(it, v1.end(), find2X); // 返回符合的迭代器(2的倍数)
if (it != v1.end())
{
cout << "Find!" << (*it) << endl;
}
cout << endl;
}
// 自定义数据
for (vector<Person>::iterator itc = vc.begin(); itc != vc.end(); itc++) // 遍历
{
// ??? find_if 返回的结束迭代器在if(itc != vc.end())中为true,在for中 itc != vc.end() ==false
itc = find_if(itc, vc.end(), findCPerson5X); // 返回符合的迭代器
if (itc != vc.end())
cout << "找到" << itc->m_Name << ":" << itc->m_Age << endl;
cout << endl;
}
运行结果及报错内容
我想要达到的结果
为什么返回的结束迭代器在if(itc != vc.end())中为true,在for中 itc != vc.end() 为false