What is a difference about inserting a value to the vector between these two methods:
vector<int> vectorlist (12);
vectorlist[12]=23;
Versus:
vector<int> vectorlist (12);
vectorlist.at(12)=23;
What is a difference about inserting a value to the vector between these two methods:
vector<int> vectorlist (12);
vectorlist[12]=23;
Versus:
vector<int> vectorlist (12);
vectorlist.at(12)=23;
.at(...) is doing bound checking, meanwhile the [] operator does not, i.e. for out of range.
See the documentation from here:
http://www.cplusplus.com/reference/vector/vector/at/
"The function automatically checks whether n is within the bounds of valid elements in the vector, throwing an out_of_range exception if it is not (i.e., if n is greater or equal than its size). This is in contrast with member operator[], that does not check against bounds."
or:
http://www.cplusplus.com/reference/vector/vector/operator[]/
"A similar member function, vector::at, has the same behavior as this operator function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception."
Slightly off-topic, but you should not use "vectorlist" term for a vector. At first, I thought you would be having a list data for some reason.
So, to give you a real world example: you could use the non-bound-checking variant when you are sure the index inside the range because that will result a slightly faster code.
According to C++ standard:
A similar member function, vector::at, has the same behavior as this operator [] function, except that vector::at is bound-checked and signals if the requested position is out of range by throwing an out_of_range exception.