我写了这一小法,只是想看一个炉子实际上如何失效,在达到其能力后不会改变病媒的位置。
这里,病媒和能力的大小在最初的5个。 之后,我插入了病媒中的其他几个要素,但没有重新开始我的发件人点到myvector.begin()
。 由此可见<代码>49在我产出中,在<代码>后,病媒最大尺寸为:1073741823时,再次印刷病媒元素。
My question is why C++ does not make point iterator again to a valid myvector.begin()
after all elements are copied into new location?
This can also lead to some behavior which can be hard to debug. I know a safe way to work would be to always reinitialize the iterator just before using it.
#include<iostream>
#include<vector>
#include<stdio.h>
using namespace std;
int main()
{
vector<int> myvector;
vector<int>::iterator it;
int myarray[]= {100,200,300,400};
myvector.insert(it,500);
it=myvector.begin();
myvector.insert(it,myarray,myarray+4);
it=myvector.begin();
for(;it!=myvector.end();++it)
cout <<*it<<endl;
cout <<"size of vector is :" << myvector.size() <<"
";
cout <<"capacity of vector is : " << myvector.capacity()<<"
";
cout <<"maximum size of vector is : " << myvector.max_size()<<"
";
myvector.push_back(600);
for(;it!=myvector.end();++it)
cout <<*it<<endl;
}
Output of program :-
100
200
300
400
500
size of vector is :5
capacity of vector is : 5
maximum size of vector is : 1073741823
49
100
200
300
400
500
600