Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ remove numbers from vector if larger than n

#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;

int main()
{
  // intitalize both the vector and the target value to be under
  vector<int> V = {2,3,5,11};
  int target = 8;
  
  // remove any numbers in the vector that is larger than the target
  vector<int> :: iterator it = remove_if(V.begin(), V.end(), bind2nd(greater<int>(), target));
  V.erase (it, V.end());
  
  // output the answer
  copy(V.begin(), V.end(), ostream_iterator<int>(cout, " "));
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #remove #numbers #vector #larger
ADD COMMENT
Topic
Name
3+6 =