• Home
  • Recent Q&A
  • Java
  • Cloud
  • JavaScript
  • Python
  • SQL
  • PHP
  • HTML
  • C++
  • Data Science
  • DBMS
  • Devops
  • Hadoop
  • Machine Learning
in C Plus Plus by
Q:

 How to get element by index in vector | at() vs operator []

1 Answer

0 votes
by

std::vector provides [] operator i.e.

1

element_reference operator[] (size_type n);

It returns the reference of element in vector at index n.

std::vector provides an another member function at() i.e.

reference at(size_type n);

It returns the reference of element at index n in vector. If index n is out of range i.e. greater then size of vector then it will throw out_of_range exception.

Related questions

+1 vote
+1 vote
asked Jul 16, 2019 in C Plus Plus by Indian
+1 vote
+1 vote
asked Jul 16, 2019 in C Plus Plus by Indian
...