C++ 带迭代器和向量的Seg故障

C++ 带迭代器和向量的Seg故障,c++,vector,C++,Vector,我似乎在用迭代器显示向量中的项时遇到问题。也许,我只是需要另一双眼睛来观察它 vector<string> tempVector; vector<string>::iterator it; it = tempVector.begin(); tempVector.push_back("1"); cout << *it; 向量tempVector; 向量::迭代器; it=tempVector.begin(); tempVector.推回(“1”); cou

我似乎在用迭代器显示向量中的项时遇到问题。也许,我只是需要另一双眼睛来观察它

vector<string> tempVector;
vector<string>::iterator it;


it = tempVector.begin();
tempVector.push_back("1");
cout << *it;
向量tempVector;
向量::迭代器;
it=tempVector.begin();
tempVector.推回(“1”);

cout调用
vector::reserve()
会使所有现有迭代器在需要重新分配时失效

引用C++标准,23.3.63[向量容量] < /P> 当且仅当当前容量小于reserve()的参数时,此时才会发生重新分配。 [...] 重新分配使序列中引用元素的所有引用、指针和迭代器无效


编辑:编辑后,您将调用
vector::push_back()
,如果需要重新分配,这也会使所有迭代器无效。可能会有帮助。

调用
vector::reserve()
会使所有现有迭代器在需要重新分配时失效

引用C++标准,23.3.63[向量容量] < /P> 当且仅当当前容量小于reserve()的参数时,此时才会发生重新分配。 [...] 重新分配使序列中引用元素的所有引用、指针和迭代器无效

编辑:编辑后,您将调用
vector::push_back()
,如果需要重新分配,这也会使所有迭代器无效。可能会有帮助。

@ChrisHarris,“使所有迭代器无效”表示如果您尝试使用以前创建的迭代器,则可能会出现seg错误。@ChrisHarris,“使所有迭代器无效”表示如果您尝试使用以前创建的迭代器,则可能会出现seg错误。