how to get the first value in vector cpp code example

Example 1: get the first element of array c++

int arr = [1,2,3,4];
cout << arr[0];			//printing the first element of the array arr

Example 2: c++ vector get first element

// vector::front
#include <iostream>
#include <vector>

int main () {

  std::vector<int> myvector;
  myvector.push_back(78);
  myvector.push_back(16);
  // now front equals 78, and back 16
  
  int first = myvector.front(); //first = 78
  std::cout << "the first value in vector is " << first << std::endl;
  return 0;
}

Tags:

Cpp Example