这个问题在这里已有答案:
我想知道为什么“a.push_back(4)”会导致运行时错误。没有“a.push_back(4)”没有运行时错误。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void main()
{
vector<int> a(5);
a.push_back(1);
a.push_back(2);
a.push_back(3);
vector<int>::iterator begIt = begin(a);
vector<int>::iterator endIt = end(a);
a.push_back(4); // Once it is removed, this program will work well.
auto begIt2 = begin(a);
auto endIt2 = end(a);
auto findIt = find(begIt, endIt, 4);
if (findIt == endIt)
cout << "not found";
else
cout << *findIt;
}