×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Different ways to access elements from a Vector in C++ STL

Here, we are going to learn how to access elements from a vector by using different methods in C++ STL?
Submitted by Yash Khandelwal, on April 20, 2019

Here are the different ways to access elements from a vector:

Using [] operator

To access vector elements, you can use the index of the elements inside the square brackets ([]).

Syntax

vector_name[index]

Parameter

index – is the position in vector.(0-based indexing)

Return Value

The element at that given index.

Example

vector<int> a;

a.push_back(1);
a.push_back(2);
a.push_back(3);

int p=a[0]; //p=1
int q=a[1]; //q=2

a[n] for any n out of index bound it reflects undefined behavior (depends on compiler).

Using function .at(index)

You can also use the .at() function by passing the index of the specific element you want to access.

It returns a reference to the element and this function automatically checks whether n is within the range or not. In case of exception it raises out of index range error.

That's the difference between .at() and [] operator does not keep any check for out of the index bounds.

Syntax

vector_name.at(pos_n)

Parameter

index of desired element

Return Value

Returns the referenced element present at input index.

Example

//On same vector a

int p=a.at(0); //p=1
int q=a.at(1); //q=2

a.at(4) //compilation error

Using front() function

You can also use the .front() function, it returns the reference to the first elements of the vector.

Syntax

vector_name.front()

Parameter

None

Return Value

Return the reference of first element of the vector.

Example

//On same vector a

int p=a.front() //p=1

Note: Calling this function on an empty vector shows undefined behavior.

Using back() function

You can also access the elements in reverse order by using the .back() function, which returns the reference to the last element of the vector.

Syntax

vector_name.back()

Parameter

None

Return: Reference of last element.

Example

//On same vector a

int q=a.back() //q=3

Note: Calling this function on an empty vector shows undefined behavior.

C++ code to demonstrate example of the functions to access vector elements

// C++ program to show
// how to access elements in vector in C++ STL>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  // declaring vector n
  vector<int> n{1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

  /* This is how the operator[i]
  in c++ works i is the index
  which is changing continuously
  i.e printing the vector is best example*/

  cout << "Output of operator[]: \n";
  for (int i = 0; i < n.size(); i++) cout << n[i] << " ";

  /* This is how at() works similar
  to the operator[] ,
  Here it changes the whole vector
  */
  cout << "\n\nOutput of at(): \n";
  for (int i = 0; i < n.size(); i++) {
    n.at(i) = i;
    cout << n.at(i) << " ";
  }
  cout << "\n\nOutput of change vector because of at(): \n";
  for (int i = 0; i < n.size(); i++) cout << n[i] << " ";

  /*This is how the front()
  works by using that here we are
  accessing the front element of vector
  */
  cout << "\n\nOutput of front(): \n";
  cout << n.front();

  /*This is how the back()
  works by using that here we are
  accessing the back element of vector
  */
  cout << "\n\nOutput of back(): \n";
  cout << n.back();

  return 0;
}

Output

Output of operator[]:
1 2 3 4 5 6 7 8 9 0

Output of at():
0 1 2 3 4 5 6 7 8 9

Output of change vector because of at():
0 1 2 3 4 5 6 7 8 9

Output of front():
0

Output of back():
9

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.