std::remove in C++ STL Last Updated : 17 Oct, 2024 Comments Improve Suggest changes Like Article Like Report The std::remove() is a built-in algorithm of the C++ STL that is used to remove an element from the specified range of elements. This range can be the whole or a part of vectors, deque, arrays, lists and forward_lists only. std::remove is defined inside <algorithm> header file.In this article, we will learn how to delete the given element from the container using std::remove() in C++.Example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 3, 5}; // Remove all occurrences of 3 from vector v auto ne = remove(v.begin(), v.end(), 3); v.erase(ne, v.end()); for (auto i: v) cout << i << " "; return 0; } Output1 2 4 5 As you may have noticed, we still had to use the vector::erase() function to actually delete the elements. This is due to the fact that std::remove() function moves all the elements to remove at the end of the container and returns an iterator to the new end of the range that only contains the elements not to remove. It doesn't actually delete these elements from the container.So, we still have to use the erase() function of the corresponding container to actually delete the elements.std::remove Syntaxstd::remove(first, last, val);Parametersfirst: Iterator to the first element of the container.last: Iterator to the element after the last element of the container.val: Value to be removed.Return ValueReturns an iterator to the new end of the modified range that only contains the elements not be deleted.If there are no elements to be deleted, returns iterator to the container's end.More Examples of std::remove()std::remove() function can be used to remove the elements from any given container or an array as demonstrated by the below examples:Example 1: Removing an Element from an Array C++ // C++ program to remove an element from an array // using std::remove #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 3, 8}; int s = sizeof(arr) / sizeof(arr[0]); // Use std::remove to shift all occurences of 3 // to the front int *ne = remove(arr, arr + s, 3); // Calculate the new size after removal int ns = ne - arr; for (int i = 0; i < ns; i++) cout << arr[i] << " "; return 0; } Output1 2 4 5 6 7 8 Example 2: Using std::remove() without Erase Function C++ // C++ Program to illustrate how to remove the #include <bits/stdc++.h> using namespace std; int main() { list<int> l = {1, 2, 3, 4, 3, 5}; cout << "Initial Size: " << l.size() << endl; // Using only std::remove() to remove all // occurrences of 3 from list auto ne = remove(l.begin(), l.end(), 3); cout << "Size After Removing 3: " << l.size(); return 0; } OutputInitial Size: 6 Size After Removing 3: 6Explanation: This program verifies that std::remove() algorithm does not actually remove the elements from the container, just shifts them. Due to this, there is no effect on the size of the container.Example 3: Trying to Remove an Element not Present in the Vector C++ // C++ program to remove an element not present in // a vector using std::remove() #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 4, 5, 6}; // Try to remove 3 that is not present auto ne = remove(v.begin(), v.end(), 3); // Erase the 'removed' part which does nothing // since 3 isn't in the vector v.erase(ne, v.end()); for (int i : v) cout << i << " "; return 0; } Output1 2 4 5 6 Comment More infoAdvertise with us Next Article std::remove in C++ STL S Sachin Bisht Improve Article Tags : C++ STL cpp-algorithm-library Practice Tags : CPPSTL Similar Reads std::remove_if in C++ STL std::remove_if() is a built-in algorithm of the C++ STL that is used to remove elements from a specified range of elements that satisfies the given condition. The condition can be defined as a function, lambda expression or a function object. It is defined inside the <algorithm> header file.In 4 min read std::remove_cv in C++ with Example The std::remove_cv template of C++ STL is present in the <type_traits> header file. The std::remove_cv template of C++ STL is used to get the type T without const and volatile qualification. It return the boolean value true if T is without const and volatile qualified, otherwise return false. 1 min read Queue in C++ STL In C++, queue container follows the FIFO (First In First Out) order of insertion and deletion. According to it, the elements that are inserted first should be removed first. This is possible by inserting elements at one end (called back) and deleting them from the other end (called front) of the dat 4 min read std::remove_volatile in C++ with Examples The std::remove_volatile template of C++ STL is present in the <type_traits> header file. The std::remove_volatile template of C++ STL is used to get the T without volatile qualification. It return the boolean value true if T is without volatile qualified, otherwise return false. Below is the 2 min read list remove() function in C++ STL The list::remove() is a built-in function in C++ STL which is used to remove elements from a list container. It removes elements comparing to a value. It takes a value as the parameter and removes all the elements from the list container whose value is equal to the value passed in the parameter of t 2 min read set::clear in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() 2 min read list::operator= in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::operator= This operator is used to assign n 2 min read list::clear() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::clear()clear() function is used to remove a 1 min read list::remove() and list::remove_if() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::remove() remove() function is used to remov 3 min read std::string::clear in C++ The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters. Parameters: none Return Value: none void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing CPP // 1 min read Like