vector和deque都提供了两个随机访问函数
进行索引操作符 operator[]
at() 检测正被索引的容器的边界
如果超出了边界抛出一个异常
//: C07:IndexingVsAt.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Comparing "at()" to operator[].
#include <ctime>
#include <deque>
#include <iostream>
#include <vector>
#include "../require.h"
using namespace std;
int main(int argc, char* argv[]) {
long count = 1000;
int sz = 1000;
if(argc >= 2) count = atoi(argv[1]);
if(argc >= 3) sz = atoi(argv[2]);
vector<int> vi(sz);
clock_t ticks = clock();
for(int i1 = 0; i1 < count; i1++)
for(int j = 0; j < sz; j++)
vi[j];
cout << "vector[] " << clock() - ticks << endl;
ticks = clock();
for(int i2 = 0; i2 < count; i2++)
for(int j = 0; j < sz; j++)
vi.at(j);
cout << "vector::at() " << clock()-ticks <<endl;
deque<int> di(sz);
ticks = clock();
for(int i3 = 0; i3 < count; i3++)
for(int j = 0; j < sz; j++)
di[j];
cout << "deque[] " << clock() - ticks << endl;
ticks = clock();
for(int i4 = 0; i4 < count; i4++)
for(int j = 0; j < sz; j++)
di.at(j);
cout << "deque::at() " << clock()-ticks <<endl;
// Demonstrate at() when you go out of bounds:
try {
di.at(vi.size() + 1);
} catch(...) {
cerr << "Exception thrown" << endl;
}
getchar();
} ///:~
输出
vector[] 39
vector::at() 39
deque[] 979
deque::at() 969
Exception thrown
不同的系统采用不同的方法来处理未捕获的异常
使用at()时,可以通过多个途径知道程序的某一部分发生了错误
可是在使用operator[]时却可能一无所知