vector<int> v;
v.reserve(10);
for(int i=0; i<7; i++)
v.push_back(i);
try
{
int iVal1 = v[7]; // not bounds checked - will not throw
int iVal2 = v.at(7); // bounds checked - will throw if out of range
}
catch(const exception& e)
{
cout << e.what();
}
参考网址http://www.cnblogs.com/yxnchinahlj/archive/2011/03/06/1972435.html
vector<string> sVec; // 空vector cout << sVec[0]; // 运行时错误:sVec中没有元素! cout << sVec.at(0); // 抛出一个out_of_range异常
见《C++Primer》P310:c[n]操作若n>=c.size(),则函数行为未定义; c.at(n)操作,如果下标越界,则抛出out_of_range异常