Why no built-in range checking with vectors?

Jul 3, 2009 at 7:35pm
According to Stroustrup in "The C++ Programming Language", "The standard library vector does not provide range checking by default." I.e.,

1
2
3
4
5
6
7
8
9
10

struct Entry {
string name i;
int number;
} ;

vector<Entry> book(1000) i;       // vector of 1000 elements

int i = phone_book[1001].number; // 1001 is out of range


does not give an error (according to the book, I've not tested this.

Why not? I understand how to handle this with at() but why is there the flexibility to allow a vector to use out-of-range indexes? What is the advantage?
Jul 3, 2009 at 8:15pm
For efficiency reasons, it aims to be as fast as an array, and it is. If you want rance changing, use at.
Topic archived. No new replies allowed.