C++ Algorithm replace()30 Aug 2024 | 3 min read C++ Algorithm replace() function is used to replace all value equal to old_value by the value new_value in the range [first, last). This function examines each element in the range and replaces it if it matches a specified value. SyntaxParameterfirst: A forward iterator pointing to the initial position in the range from which elements are being replaced. last: A forward iterator pointing to the final position in the range from which elements are being replaced. old_value: The old value of the elements to be replaced. new_value: The new value being assigned to the elements with the old value. Return valueNone ComplexityComplexity is linear in the distance within first and last. It compares each element and assigns to those matching. Data racesThe objects in the range [first1, last1) are accessed and potentially modified. Exception safetyThrows an exception if any of the function calls the 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(): Output: 3,10,2,10,2, In the above example, element 1 of vector v is replaced by 10. Example 2Let'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 ). The vector v1 with a value 700 replacing that of 7 is: ( 4 700 700 700 0 5 700 1 6 9 3 700 8 2 ). In the above example, replace() finds all the elements from vector v1 matches with 7 and replace it with the 700. Example 3Let's see another simple example: Output: v : 1 4 3 2 3 10 7 9 3 8 After replacing 3 with 6 v : 1 4 6 2 6 10 7 9 6 8 Example 4Let's see another simple example: Output: myvector contains: 10 99 30 30 99 10 10 99 Next TopicC++ Algorithm |
C++ Algorithm partial_sort() C++ Algorithm partial_sort() function 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 the middle and last will be in an unspecified order. The...
6 min read
C++ Algorithm lower_bound() C++ Algorithm lower_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in an ordered range [first, last) that is not less than (i.e. greater than or equal to) to the specified value...
4 min read
C++ Algorithm Function search() C++ Algorithm search() function searches the range [first1, last1) for the occurrence of a subsequence defined by the range [first2, last2), and an iterator to the first element is returned. If the subsequence does not exist then an iterator to the last1 is...
2 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 fill_n() C++ Algorithm fill_n() function is used to assign a new value to a specified number of elements in a range beginning with a particular element. It means in fill_n(), we specify beginning position, number of elements to be filled and value to be filled. Syntax template <class...
4 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 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 partition_copy() C++ Algorithm partition_copy() function is used to copy the elements for which a condition is true to one destination, and false to another. The elements must belong to a specified range. Syntax template <class InputIterator, class OutputIterator1, ...
4 min read
C++ Algorithm equal_range() C++ Algorithm equal_range() function is the version of binary search. This function is used to return the lower bound and upper bound of the sub range that includes all the elements equivalent to val in the range [first, last). Where sub range is defined...
7 min read
C++ Algorithm merge() C++ Algorithm merge() function is used to merge two sorted ranges [first1, last1) and [first2, last2) into one sorted range beginning at result. Elements are compared using operator < for the first version or using the given binary comparison function comp for the second...
6 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