Differnece between oprator[] and "at" function of vector
What’s the difference between the following statement? Can you predict?
vector<int> vec;
vec.push_back(5);
vec[0] = 10; // Statement A
vec.at(0) = 11; // Statement B
How statement A and B differs?
Basically, both of them are doing same thing. i.e access the members at a specified location.
But vec.at(0) will be more safe than operator[]. If we are accessing out-of-boundary elements of container(vector), it will throw std::out_of_range exception on calling “at” function.
As per the standard specification, operator[] of vector container will not throw any exception if we are accessing out-of-boundary elements.
If you do something as follows, you program will get crashed.
i.e
vector<int> v; // not initialized
v[10] = 10; // Crashes here.
——————————————
vector<int> v; // not initialized
v.at(10) = 10; // safe if we catch the exception
OK. Then why vector provides two functions to do the same thing? Yea! it’s for performance. because “at” function will check the overflow and underflow of the index specified with it’s size. So on each time you calling the function, this checking will happen, which is a performance glitch on some context. Moreover vectors are considered like arrays so it should facilitate element access as fast as arrays do(have you heard any array that do boundary checking?).
int nSize = v.size();
for(int i =0;i<nSize;i++)
cout<<v[i]; // This will be faster than “at” function
In the above code there’s no chance(normal case) of failure so that we can simply access the elements using operator[] which will make the iterations faster
The following code will be ideal in the case of “at” function
try
{
vector<int> v;
v.at(10) = 10;
}
catch(std::out_of_range)
{
cout<<”out of range”;
}
Let me conclude. If you are sure that element is existing there, you can access it by operator[], and if you are unsure call “at” function enclosed within a try catch block
One more thing: The STL vendors can offers exception mechanism with the libraries. So depending on the vendor, operator[] may throw exception. Visual C++ 2005 libraries are secure. If we enable _SECURE_SCL macro(checked iterators), these functions will be throwing exceptions.
You can enable Checked iterators by definining #define _SECURE_SCL 1
To disable #define _SECURE_SCL 0
Thanks Heb Sutter for your wonderful book called “Exceptional C++”
