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

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

C++ Vector Operation Delete Elements

#include <iostream>
#include <vector>

using namespace std;

int main() {
  vector<int> num{1, 2, 3, 4, 5};
  
  // initial vector
  cout << "Initial Vector: ";
  for (int i : num) {
    cout << i << " ";
  }

  // remove the last element
  num.pop_back();

  // final vector
  cout << "
Updated Vector: ";
  for (int i : num) {
    cout << i << " ";
  }
  
  return 0;
}
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 :: how to empty string c++ 
Cpp :: power function c++ 
Cpp :: string length in c++ 
Cpp :: setw c++ 
Cpp :: how to calculate bitwise xor c++ 
Cpp :: cpp class constructor 
Cpp :: c++ reverse string 
Cpp :: intersection.cpp 
Cpp :: pure virtual function in c++ 
Cpp :: cpp string find all occurence 
Cpp :: word equation numbers 
Cpp :: C++ Infinite while loop 
Cpp :: c++ doubly linked list 
Cpp :: input full line as input in cpp 
Cpp :: ++ how to write quotation mark in a string 
Cpp :: 31. Next Permutation leetcode solution in c++ 
Cpp :: how to use a non const function from a const function 
Cpp :: how to replace part of string with new string c++ 
Cpp :: is anagram c++ 
Cpp :: overload array operator cpp 
Cpp :: selection sort c++ 
Cpp :: ue4 c++ replicate actor variable 
Cpp :: QVariant to int 
Cpp :: basic cpp 
Cpp :: find the graph is minimal spanig tree or not 
Cpp :: volumeof a sphere 
Cpp :: modular exponentiation algorithm c++ 
Cpp :: shortest path in unweighted graph bfs 
Cpp :: c++ sorting and keeping track of indexes 
Cpp :: the difference between i++ and ++i 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =