Removing an element from C++ std::vector<> by index?

C++Server Side ProgrammingProgramming

Remove an element from C++ std::vector<> by index can be done by following way −

Example

 Live Demo

#include<iostream>
#include<vector>
using namespace std;
int main() {
   vector<int> v; //declare vector
   //insert elements into vector
   v.push_back(-10);
   v.push_back(7);
   v.push_back(6);
   // Deletes the first element (v[0])
   v.erase(v.begin() );
   for (int i = 0; i < v.size(); i++)
      cout << v[i] << " ";
}

Output

7 6
raja
Published on 18-Jun-2019 12:04:13
Advertisements