C++ Algorithm remove_if()30 Aug 2024 | 4 min read C++ Algorithm remove_if() function is used to eliminate all the elements that satisfy a predicate from a given range [first, last) without disturbing the order of the remaining elements.
SyntaxParameterfirst: A forward iterator pointing the position of the first element in the range from which elements are being removed. last: A forward iterator pointing the position one past the final element in the range from which elements are being removed. pred: The unary predicate function which accepts an element as an argument that must be satisfied is the value of an element is to be replaced. Return valueA forward iterator pointing the new end position (last) of the modified range or first element if first and last is equal. ComplexityComplexity is linear in the range [first, last): Applies pred to each element, and possibly performs assignments on some of them. Data racesThe object in the range [first, last) are accessed and potentially modified. Exception safetyThis function throws an exception if any of pred, the element assignments or the operation on an iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of remove_if(): Output: Original vector : 1 2 3 4 5 6 7 8 9 10 After remove_if : 2 4 6 8 10 Example 2Let's see another simple example: Output: Vector v1 is ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 ). Vector v1 with elements satisfying greater6 removed is ( 4 0 5 1 6 3 2 1 6 9 3 7 8 2 ). Vector v1 resized elements satisfying greater6 removed is ( 4 0 5 1 6 3 2 ). Example 3Let's see another simple example: Output: Vector : China India Korea America Australia Pakistan Vector : China India Korea Pakistan Example 4Let's see another simple example to remove all spaces from a string by shifting all non-space characters to the left and then erasing the extra space. Output: Textwithsomespaces Textwithsomewhitespaces Next TopicC++ Algorithm |
algorithm copy_n() function
C++ Algorithm Functions copy_n() C++ Algorithm copy_n() function specifies the number of elements to be copied into the new container. The function is used to copy n elements of the container [first,last) into a different container starting from result. Syntax template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n(InputIterator first, Size...
1 min read
algorithm move_backward() function
C++ Algorithm Functions move backward () The function is used for moving of elements in the backward order, it accepts three arguments and then moves the elements belonging to the range [first,last). The moving of elements begins in the reverse order with termination point at 'result'. Syntax template<class BidirectionalIterator1,...
2 min read
algorithm count_if() function
C++ Algorithm Function count_if () C++ Algorithm count_if() function has a 'pred' value and returns the count of the elements in the range [first,last) for which the value of pred is true. Syntax template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last,UnaryPredicate pred); Parameter first: It is an input iterator...
1 min read
algorithm copy_backward() function
C++ Algorithm Functions copy_backward() C++ Algorithm copy_backward() function is used for copying of elements in the backward order, it accepts three arguments and then copies the elements belonging to the range [first,last]. The copying of elements begins in the reverse order with termination point at 'result'. Syntax template<class BidirectionalIterator1,...
2 min read
algorithm unique_copy() function
C++ Algorithm unique_copy() C++ Algorithm unique_copy() function is used to copy a sequence such as each duplicate consecutive element becomes a unique element. It will not alter the original range and copy the result into another container. The first version uses operator== to compare the elements and the second...
6 min read
C++ Algorithm max() Function
C++ Algorithm max() C++ Algorithm max() function can be used in following 3 ways: It compares the two values passed in its arguments and returns the larger between them. If both are equal, then it returns the first one. It also compares the two values using a binary...
5 min read
algorithm remove_copy() function
C++ Algorithm remove_copy() C++ Algorithm remove_copy() function is used to copy all elements which are not equal to val from the range [first, last) to provide result without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator...
4 min read
algorithm partial_sort_copy() function
C++ Algorithm partial_sort_copy() C++ Algorithm partial_sort_copy() function is similar to partial_sort() function which is used to rearrange the elements in the range[first, last), in such a way that the elements between the first and middle will be sorted and the elements between middle and last will be...
8 min read
algorithm equal() function
C++ Algorithm Function equal() C++ Algorithm equal()function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching. The first range is from [first1,last1) and the second starts from first2. Syntax template<class InputIterator1, class...
2 min read
algorithm iter_swap() function
C++ Algorithm iter_swap() C++ Algorithm iter_swap() exchanges the elements pointed to by two iterators a and b. Syntax template <class ForwardIterator1, class ForwardIterator2> void iter_swap (ForwardIterator1 a, ForwardIterator2 b); Parameter a: One of the forward iterator whose value is to be exchanged. b: Second of the forward iterator whose value is...
7 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India