#include <algorithm>
#include <vector>
// using the erase-remove idiom
std::vector<int> vec {2, 4, 6, 8};
int value = 8 // value to be removed
vec.erase(std::remove(vec.begin(), vec.end(), value), vec.end());
//me
vec.erase(vec.begin() + index); //index 0 means first element and so on
vector.erase(position) // remove certain position
// or
vector.erase(left,right) // remove positions within range
// erase element from vector by its index
vector<string> strs {"first", "second", "third", "last"};
string element = "third"; // the element which will be erased
for(int i=0;i<strs.size();i++)
{
if(strs[i] == element)
strs.erase(strs.begin()+i);
}
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> num{1, 2, 3, 4, 5};
// initial vector
cout << "Initial Vector: ";
for (int i : num) {
cout << i << " ";
}
// remove the last element
num.pop_back();
// final vector
cout << "
Updated Vector: ";
for (int i : num) {
cout << i << " ";
}
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main(){
//Creation of integer vector
vector<int> vectorArray ;
for(int i=1;i<10;i++){
vectorArray.push_back(i);
}
//vector elements are 1,2,3,4,5,6,7,8,9
vectorArray.pop_back();
for(int i=0;i<vectorArray.size();i++){
cout<<vectorArray[i]<<" ";
}
//vector elements are 1,2,3,4,5,6,7,8
cout<<endl;
vectorArray.clear();
// No elements are left in vector array
return 0;
}