Search
 
SCRIPT & CODE EXAMPLE
 

CPP

remove value from vector c++

#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());
Comment

How to remove an element from a vector using remove_if

void RemovePet(std::vector<Pet> & pets, std::string name) {
    pets.erase(
        std::remove_if(pets.begin(), pets.end(), [&](Pet const & pet) {
            return pet.Name == name;
        }),
        pets.end());
}
Comment

C++ REMOVE element from vector

//me
vec.erase(vec.begin() + index); 	//index 0 means first element and so on
Comment

c++ remove element from vector

vector.erase(position) // remove certain position
// or
vector.erase(left,right) // remove positions within range
Comment

remove element from vector



// 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);
    }
    
Comment

remove elements from vector

#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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ constructor 
Cpp :: bitmap 
Cpp :: linkedlist in c++ 
Cpp :: reverse in vector c++ 
Cpp :: double plus overload 
Cpp :: c++ map vector as keys 
Cpp :: ue4 endoverlap c++ 
Cpp :: gcd of two numbers 
Cpp :: c++ void poiinter 
Cpp :: c++ tuple push_back 
Cpp :: wgat is duble in c++ 
Cpp :: c++ *agrs 
Cpp :: subtraction of a 2d matrix in c++ 
Cpp :: how to make c++ read strlen 
Cpp :: cplusplusbtutotrail 
Cpp :: cap phat dong mang 2 chieu trong c++ 
Cpp :: setFontSize QT 
Cpp :: argsort c++ 
Cpp :: coin change top-down 
Cpp :: youtube to facebook link converter 
Cpp :: vector with initial size 
Cpp :: cpp stacks 
Cpp :: cin une énumération 
Cpp :: c++ arrays 
Cpp :: atomic int c++ add 1 
Cpp :: stack using cpp 
Cpp :: convert c program to c ++ online 
Cpp :: find number of 1s in a binary cv::mat image 
Cpp :: c++ program to convert kelvin to celsius 
Cpp :: c++ How can I make a std::vector of function pointers 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =