std::remove_if in C++ STL Last Updated : 17 Oct, 2024 Comments Improve Suggest changes Like Article Like Report 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 this article, we will learn how to remove elements from a container based on a condition using std::remove_if in C++.Example: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5, 6}; // Remove all even numbers from vector v auto ne = remove_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; }); v.erase(ne, v.end()); for (auto i : v) cout << i << " "; return 0; } Output1 3 5 Just like std::remove() algorithm, std::remove_if() doesn't actually delete the elements from the container. It only moves the elements that satisfies the given condition to the end of the container and returns an iterator to the new end of the range that contains the elements that should be kept. We need to use the erase function of corresponding container (if any) with std::remove_if() to actually delete the elements.std::remove_if Syntaxstd::remove_if(first, last, p);Parametersfirst: Iterator to the first element of the range.last: Iterator to the element after the last element of the range.p: A function, lambda or a functor that returns true for elements that should be removed. It is called a predicate function.Return ValueReturns an iterator to the new end of the modified range that only contains the elements not to be deleted.If no elements match the condition, it returns an iterator to the container's end.Supported Containersstd::remove_if() can be used with containers that provide forward iterators or better, such as:std::vectorstd::dequestd::liststd::forward_listRaw arraysMore Examples of std::remove_ifThe std::remove_if() function can be used to remove elements based on a condition from various containers or arrays, as demonstrated by the examples below:Example 1: Removing Elements from an Array Using Lambda Expression C++ // C++ program to remove elements from an array // using std::remove_if #include <bits/stdc++.h> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; int s = sizeof(arr) / sizeof(arr[0]); // Remove all odd numbers using std::remove_if int* ne = remove_if(arr, arr + s, [](int x) { return x % 2 != 0; }); // Calculate the new size after removal int ns = ne - arr; for (int i = 0; i < ns; i++) cout << arr[i] << " "; return 0; } Output2 4 6 8 Example 2: Removing Elements from Deque Using Traditional Function C++ // C++ Program to illustrate use of std::remove_if // wihthout erase function #include <bits/stdc++.h> using namespace std; // Function that returns true for integer // greater than 20 bool f(int a) { return a > 20; } int main() { deque<int> d = {10, 15, 20, 25, 30, 35}; // Remove elements greater than 20 from // the deque auto ne = remove_if(d.begin(), d.end(), f); // Actually removing the element d.erase(ne, d.end()); for (auto i: d) cout << i << " "; return 0; } OutputInitial Size: 6 Size After Removal: 6 Comment More infoAdvertise with us Next Article std::remove_if in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL cpp-algorithm-library Practice Tags : CPPSTL Similar Reads 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 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 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() 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 is_object Template in C++ The std::is_object template of C++ STL is used to check whether the given type is object or not. It returns a boolean value showing the same. Syntax: template <class T > struct is_object; Parameter: This template accepts a single parameter T (Trait class) to check whether T is a object type or 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 set::empty() 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::empty() empty() 2 min read std::find_if , std::find_if_not in C++ std :: find_if and std :: find_if_not are algorithm functions in C++ Standard Library in <algorithm> header file. These functions provide an efficient way to search for an element in a container using a predicate function. std :: find_if This function returns an iterator to the first element i 3 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 Erase-Remove Idiom in C++ Erase-Remove-Idiom is a C++ STL (Standard Template Library) technique to remove elements from a container. It is used to remove all the elements satisfying certain conditions from the container. The erase-remove idiom is especially useful in array-based containers like vectors, where each eliminatio 4 min read Like