C++ Algorithm remove_copy_if()30 Aug 2024 | 5 min read C++ Algorithm remove_copy_if() function is used to copy all elements in the range [first, last) to the range beginning at result, except those elements for which pred returns true without disturbing the order of the remaining elements. This function cannot alter the size of the container.
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. result: An output iterator pointing to the initial position of the range to which elements are being removed. pred: 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 copied range, which includes all elements in [first, last) except those for which pred will return true. ComplexityComplexity is linear in the range [first, last): Applies pred to each element, and performs assignment operation for those which are not removed. Data racesThe object in the range [first, last) are accessed. The objects in the range between result and the returned value are changed. ExceptionsThis function throws an exception if any of pred, the element assignments or the operations on iterator throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of remove_copy_if(): Output: 2,4,6,8, Example 2Let's see another simple example: Output: elements of v1 before remove_copy: 10 11 12 13 14 15 16 17 18 19 20 elements of v1 after remove_copy: 10 11 12 13 14 15 16 17 18 19 20 After removing Odd Numbers from v1 copy result in vector v2 10 12 14 16 18 20 0 0 0 0 Example 3Let's see another simple example: Output: Numbers { 10 20 10 15 12 25 30 10 } Total number of elements copied to Result = 6 Result { 10 20 10 15 12 10 0 0 } Example 4Let's see another simple example: Output: The original vec1 vector data: 0 1 2 3 4 5 6 7 8 9 10 5 5 5 The original vec1 vector data randomly shuffled: 4 10 5 5 0 5 5 1 6 9 3 7 8 2 After the remove_copy_if() operation, the vec1 vector is left unchanged as: 4 10 5 5 0 5 5 1 6 9 3 7 8 2 vec2 vector is a copy of vec1 vector with values greater than 7 removed: 4 5 5 0 5 5 1 6 3 7 2 Next TopicC++ Algorithm |
C++ Algorithm replace_if() C++ Algorithm replace_if() function is used to assign new_value to all the elements in the range [first, last) for which pred predicate returns true. This function examines each element in a range and replaces it if it satisfies a specified predicate. Syntax template <class ForwardIterator,...
4 min read
C++ Algorithm binary_search() C++ Algorithm binary_search() function is used check whether the element in the range [first, last) is equivalent to val (or a binary predicate) and false otherwise. The range [first, last) must satisfy all of the following conditions: Partitioned with respect to element < val or comp...
5 min read
C++ Algorithm fill() C++ Algorithm fill() function is used to assign the same new value to every element in a specified range[first, end) by using operator=. Note: Range [first, last) means first is included in the range but last is not included. Syntax template <class ForwardIterator, class T> void...
4 min read
C++ Algorithm shuffle() C++ Algorithm shuffle() function reorders the elements of a range by putting them at random places, using g as uniform random number generator. Syntax template <class RandomAccessIterator, class URNG> void shuffle (RandomAccessIterator first, RandomAccessIterator last, URNG&& g); Parameter first: A random access iterator pointing the position of...
2 min read
C++ Algorithm replace_copy_if() C++ Algorithm replace_copy_if() function is used to make a copy of the range [first, last) to the range beginning at result, placing those for which pred returns true by new_value. It uses predicate pred instead of operator== to compare the elements. This function examines each...
8 min read
C++ Algorithm is_partitioned() C++ Algorithm is_partitioned() is used to test to see if a range [first, last) is partitioned according to a predicate. In other words, all the elements in the range that satisfies the predicate are at the beginning of the sequence. If the range is empty...
4 min read
C++ Algorithm swap() Function C++ Algorithm swap() function swaps or say interchanges the values of two containers under reference. Syntax template<class T> void swap(T& a, T& b); Parameter a: It is the first container with some value. b: It is another container with some value. Return value The function only swaps the values of...
2 min read
C++ Algorithm partition() C++ Algorithm partition() function is used to make partition the elements on the basis of given predicate (condition) mentioned in its arguments. If the container is partitioned then this function returns true, else returns false. Syntax template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate...
5 min read
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 unique() C++ Algorithm unique() function is used to transform a sequence in such a way that each duplicate consecutive element becomes a unique element. The first version uses operator== to compare the elements and the second version uses the given binary predicate pred. Syntax equality (1) template <class...
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