0

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;
Mat
  • 202,337
  • 40
  • 393
  • 406
Infintyyy
  • 929
  • 1
  • 11
  • 23
  • 4
    Neither of these should be used to *insert* something. In your case, you'll see the difference just by running your code (unless the UB causes the exact same thing to happen as the other case). – chris Aug 17 '13 at 08:53
  • I think the second one will throw an exception and the first one will have undefined behaviour, because the vector size is 12 elements, not 13. Maybe the capacity will be larger though. – Benoit Aug 17 '13 at 08:55
  • @IvanaGajic - See this question regading `at`. – jww Nov 07 '16 at 01:41

2 Answers2

5

.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.

Jake Petroules
  • 23,472
  • 35
  • 144
  • 225
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • 1
    +1 just one nitpick: the cpluplus.com site contains a lot of errors, even if it is correct for this answer. Prefer to give quotes from the (freely available draft) Standard itself, or at least from better reference sites such as en.cppreference.com – TemplateRex Aug 18 '13 at 08:57
  • Stackoverflow aims to give authorative answers; for C++ there is only one authorative source, the Standard. It really isn't that hard to get answers for simple API questions from it. – TemplateRex Aug 18 '13 at 09:07
  • no in this case it's fine. but non-authorative sites like wikipedia or cplusplus.com should only be used to give example programs. – TemplateRex Aug 18 '13 at 09:17
  • @TemplateRex: My opinion is that we should fix real issues rather than nitpicking about 1-2 and miss the fixes for many others. I think they call it "pragmatic". Note, en.cppreference.com being better is your or perhaps some other people's judgement, only. ;-) – László Papp Aug 18 '13 at 09:23
0

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.

Vitalii
  • 4,434
  • 4
  • 35
  • 77