刚初始化一个vector对象时,注意其capacity和size的大小 。
容量太小而去操作会造成缓冲区溢出。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<int> ivec;
cout<<ivec.capacity()<<" "<<ivec.size()<<endl; //0 0
//注释的会造成缓冲区溢出
// for(decltype(ivec.size()) ix = 0; ix != 10; ++ix )
// ivec[ix] = ix;
for(decltype(ivec.size()) ix = 0; ix != 10; ++ix )
ivec.push_back(ix);
cout<<ivec.capacity()<<" "<<ivec.size()<<endl; //16 10
return 0;
}