How to Remove Element From Vector in C++

Jinku Hu Feb 02, 2024
  1. Use the erase() Method to Remove Element From Vector in C++
  2. Use the std::erase() Method to Remove Element From Vector in C++
  3. Use std::erase() and std::remove() to Remove Element From Vector in C++
How to Remove Element From Vector in C++

This article will explain several methods of how to remove an element from a vector in C++.

Use the erase() Method to Remove Element From Vector in C++

The erase() method is a member function of the std::vector class and is capable of a single element of the vector or the range specified as [first, last]. The function returns an iterator following the last removed element. If a single element is removed, as shown in the following code sample, the iterator passed must be dereferenceable.

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

void PrintVec(vector<string> &vec) {
  for (const auto &item : vec) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  vector<string> str_vec = {"array", "vector", "deque", "list",     "set",
                            "map",   "stack",  "queue", "multimap", "span"};
  PrintVec(str_vec);

  // DELETE element "set"
  auto elem_to_remove = str_vec.begin() + 4;
  if (elem_to_remove != str_vec.end()) {
    str_vec.erase(elem_to_remove);
  }

  PrintVec(str_vec);
  return EXIT_SUCCESS;
}

Output:

array; vector; deque; list; set; map; stack; queue; multimap; span;
array; vector; deque; list; map; stack; queue; multimap; span;

Use the std::erase() Method to Remove Element From Vector in C++

This std::erase() is a non-member function that takes the range and the value that is compared with every element to delete each time it’s matched. This method might have a disadvantage when a single element needs to be removed, but you can avoid this behavior by passing the second argument by array variable - arr[index]. As demonstrated in the next example, erase only removes the element array.

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::erase;
using std::string;
using std::vector;

void PrintVec(vector<string> &vec) {
  for (const auto &item : vec) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  vector<string> str_vec = {"array", "vector", "deque", "list",     "set",
                            "map",   "stack",  "queue", "multimap", "span"};

  PrintVec(str_vec);

  // DELETE elemen "array"
  erase(str_vec, str_vec[0]);

  PrintVec(str_vec);
  return EXIT_SUCCESS;
}

Output:

array; vector; deque; list; set; map; stack; queue; multimap; span;
vector; deque; list; set; map; stack; queue; multimap; span;

Use std::erase() and std::remove() to Remove Element From Vector in C++

This method is called Erase-remove idiom, and it removes every element that is equal to a certain value or satisfies a criterion from a given range. Notice that this solution has some exceptional features, like - it can’t be used with containers that return const_iterator.

#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::erase;
using std::string;
using std::vector;

void PrintVec(vector<string> &vec) {
  for (const auto &item : vec) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  vector<string> str_vec = {"map", "vector", "deque", "list", "set",
                            "map", "stack",  "queue", "map",  "span"};

  PrintVec(str_vec);

  // DELETES all elements with value "map"
  str_vec.erase(std::remove(str_vec.begin(), str_vec.end(), "map"),
                str_vec.end());

  PrintVec(str_vec);
  return EXIT_SUCCESS;
}

Output:

map; vector; deque; list; set; map; stack; queue; map; span;
vector; deque; list; set; stack; queue; span;
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ Vector