Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing elements of a vector in C++?

Tags:

c++

vector

I often found people use the array brackets [] and a normal vector function .at (). Why are there two separate methods? What are the benefits and disadvantages of both? I know that .at () is safer, but are there any situations where .at () cannot be used? And if .at () is always safer, why ever use array brackets [].

I searched around but couldn't find a similar question. If a questions like this already exists please forward me to it and I will delete this question.

like image 442
fdh Avatar asked Nov 22 '11 03:11

fdh


People also ask

How do you access vector elements?

Element access: reference operator [g] – Returns a reference to the element at position 'g' in the vector. at(g) – Returns a reference to the element at position 'g' in the vector. front() – Returns a reference to the first element in the vector. back() – Returns a reference to the last element in the vector.

How do you pop an element from a vector?

pop_back() function is used to pop or remove elements from a vector from the back. The value is removed from the vector from the end, and the container size is decreased by 1. 1.


3 Answers

std::vector::at() guards you against accessing array elements out of bounds by throwing a std::out_of_range exception unlike the [] operator which does not warn or throw exceptions when accessing beyond the vector bounds.

std::vector is/was considered as a C++ replacement/construct for Variable Length Arrays(VLA) in c99. In order for c-style arrays to be easily replacable by std::vector it was needed that vectors provide a similar interface as that of an array, hence vector provides a [] operator for accessing its elements. At the same time, the C++ standards committee perhaps also felt the need for providing additional safety for std::vector over c-style arrays and hence they also provided std::vector::at() method which provides it.

Naturally, the std::vector::at() method checks for the size of the vector before dereferencing it and that will be a little overhead (perhaps negligible in most use cases) over accessing elements by [], So std::vector provides you both the options to be safe or to be faster at expense of managing the safety yourself.

like image 119
Alok Save Avatar answered Oct 10 '22 23:10

Alok Save


As others have mentioned, at() performs bounds checking and [] does not. Two reasons I can think of to prefer [] are:

  1. Cleaner syntax
  2. Performance. When looping through the elements of a vector, it is often overkill and very costly to perform bounds checking on every iteration.
like image 44
Jason B Avatar answered Oct 10 '22 23:10

Jason B


at()

Pros:

  • safe because exception is thrown if array out of bounds

Cons:

  • slow access
  • more characters to type

operator[]

Pros:

  • fast access because of missing bounds checks
  • less characters to type
  • 'intuitive' array element access

Cons:

  • unsafe because of missing bounds checks
like image 41
Frank Avatar answered Oct 10 '22 23:10

Frank