C++ Algorithm replace_copy()30 Aug 2024 | 4 min read C++ Algorithm replace_copy() function is used to make a copy of the range [first, last) and replaces all old_value by the value new_value into it. It uses operator= to make the copy and to compare the elements it uses operator==. This function examines each element in a source range and replaces it if it matches a specified value while copying the result into a new destination range. SyntaxParameterfirst: An input iterator pointing to the initial position in the range from which elements are being replaced. last: A input iterator pointing to the final position in the range from which elements are being replaced. result: An output iterator pointing to the first element of the range where the resulting sequence is stored. old_value: The old value of the element being replaced. new_value: The new value assigned to the elements with the old value. Return valuereplace_copy() function returns an output iterator pointing to the position that points to the last element written in the result sequence. ComplexityComplexity is linear in the distance within first and last and performs a comparison and an assignment for each element. Data racesThe objects in the range [first1, last1) are accessed. The objects in the range within result and the returned value are modified. Exception safetyThis function throws an exception if any of the element comparison, the element assignments or the operations on iterators throws an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of replace_copy(): Output: 3,10,2,10,2, Example 2Let's see another simple example: Output: myvector contains: 10 99 30 15 99 10 10 99 Example 3Let's see another simple example: Output: The original vector v1 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ). The vector v1 with a value 70 replacing that of 7 is: ( 4 7 7 7 0 5 7 1 6 9 3 7 8 2 1 4 70 70 70 0 5 70 1 6 9 3 70 8 2 1 ). The list copy L1 of v1 with the value 0 replacing that of 7 is: ( 4 1 1 1 0 5 1 1 6 9 3 1 8 2 0 ). Example 4Let's see another simple example: Output: Before replace_copy : A B C D E F G After replace_copy : Z B C D E F G In the above example, function replace_copy() is used to replace 'A' at v.begin() with Z and copy it to v.begin() position. Next TopicC++ Algorithm |
C++ Algorithm partition_point() C++ Algorithm partition_point() function is used to return the first element in the given range for which pred is not true. The elements are sorted in a way that the elements which satisfy the condition come before those who do not satisfy. Syntax template <class...
5 min read
C++ Algorithm Functions search_n() C++ Algorithm search_n() function searches the container [first,last) for the occurrence of a sequence of count elements, that is every element is searched to check whether it satisfies a given pred. An iterator to first element satifisying the condition is returned, else an...
2 min read
C++ Algorithm random_shuffle() C++ Algorithm random_shuffle() reorders the elements of a range by putting them at random places. The first version uses an internal random number generator and the second version uses a random number generator which is a special kind of function object that is explicitly...
4 min read
C++ Algorithm Function find_if() C++ Algorithm find_if() function returns the value of the first element in the range for which the pred value is true otherwise the last element of the range is given. Syntax template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies...
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 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
C++ Algorithm Function for_each() C++ Algorithm for_each() function applies the function func to all of the elements in the range from 'first' to 'last'. Syntax template <class InputIterator, class Function> Function for_each (InputIterator first, InputIterator last, Function func); Parameter first: It specifies the first element in the list. last: It specifies the...
4 min read
C++ Algorithm remove_copy_if() 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...
6 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 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
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