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 |
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 rotate() function
C++ Algorithm rotate() C++ Algorithm rotate() function is used to rotate the order of the elements within a range [first, last). The sequence will start at the element in the middle of the source sequence and the last element will be followed by first. middle to the elements between...
7 min read
algorithm nth_element() function
C++ Algorithm nth_element() C++ Algorithm nth_element() function is used to sort the elements between the first and nth element in ascending order and element between nth and last are not sorted. However, no element in between nth and last is smaller than an element between first and...
6 min read
algorithm partition_point() function
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
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 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 find_if() function
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
algorithm move() function
C++ Algorithm Function move () C++ Algorithm move()function is used for moving the elements. It accepts three arguments and then moves the elements belonging to the range [first,last) into a range that starts with 'result'. Syntax template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is...
2 min read
algorithm stable_sort() function
C++ Algorithm stable_sort() C++ Algorithm stable_sort() function is used to sort the elements in the range [first, last) into ascending order like sort but keeps the order of equivalent elements. The elements are compared using operator < for the first version, and comp for the second version. Syntax template <class...
6 min read
algorithm unique() function
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