How to remove element from vector by value in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>
 
int main ()
{
    std::vector<int> v = { 5, 2, 7, 1, 9, 2, 4, 2 };

    v.erase(std::remove(v.begin(), v.end(), 2), v.end());
      
    for (auto const &n: v) {
        std::cout << n << " ";
    }
      
    return 0;
}
    
    
    
    
/*
run:
  
5 7 1 9 4 
     
*/

 



answered May 11, 2021 by avibootz
...