remove device with value in vector c++ code example
Example 1: how to delete an element in vector pair in cpp
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int main()
{
vector< pair<int, int> > v;
int N = 5;
const int threshold = 2;
for(int i = 0; i < N; ++i)
v.push_back(make_pair(i, i));
int i = 0;
while(i < v.size())
if (v[i].second > threshold)
v.erase(v.begin() + i);
else
i++;
for(int i = 0; i < v.size(); ++i)
cout << "(" << v[i].first << ", " << v[i].second << ")\n";
cout << "Done" << endl;
}
Example 2: remove element by value vector c++
#include<bits/stdc++.h>
using namespace std;
int main(){
vector<int> v;
v.push_back(20);
v.push_back(10);
v.push_back(30);
v.push_back(20);
v.push_back(40);
v.push_back(20);
v.push_back(10);
vector<int>::iterator new_end;
new_end = remove(v.begin(), v.end(), 20);
for(int i=0;i<v.size(); i++){
cout << v[i] << " ";
}
return 0;
}
C++Copy