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 :: reverse sort a vector 
Cpp :: c++ map insert 
Cpp :: new c++ 
Cpp :: c++ uint32_t 
Cpp :: convert characters to lowercase c++ 
Cpp :: Find the biggest element in the array 
Cpp :: Search Insert Position leetcode solution in cpp 
Cpp :: copying a set to vector in c++ 
Cpp :: creare array con c++ 
Cpp :: unordered_map contains key 
Cpp :: c++ compile to exe command line 
Cpp :: c++ thread 
Cpp :: c++ lettura file 
Cpp :: c++ split string by space into array 
Cpp :: c++ exceptions 
Cpp :: function overriding in c++ 
Cpp :: swap in cpp 
Cpp :: count sort algorithm 
Cpp :: heap buffer overflow in c 
Cpp :: overload subscript operator cpp 
Cpp :: c++ find index of element in array 
Cpp :: how to get euler constant in c++ 
Cpp :: c++ string_t to string 
Cpp :: z transfrom mathlab 
Cpp :: converting char to integer c++ 
Cpp :: stoi in c++ 
Cpp :: Abstract factory C++ code 
Cpp :: pow without math.h 
Cpp :: erase range vector c++ 
Cpp :: free a pointer c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =