C++ Algorithm replace_if()30 Aug 2024 | 4 min read 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. 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. pred: The unary predicate function that must be satisfied is the value of the element is to be replaced. new_value: The new value assigned to the elements whose old value satisfies the predicate. Return valueNone ComplexityComplexity is linear in the distance within first and last. Applies pred to each element and assigns to those matching also. Data racesThe objects in the range [first1, last1) are accessed and potentially modified. Exception safetyThis function throws an exception if any of the pred, 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_if(): Output: 10,10,2,10,2, The above example determines odd numbers from vector v and replaces all found element with 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 70 replacing those elements satisfying the greater6 predicate is: ( 4 70 70 70 0 5 70 1 6 70 3 70 70 2 ). In the above example, vector v1 with a value 70 replacing those elements satisfying the greater 6 predicate. Example 3Let's see another simple example: Output: Here are the values in the vector: 1 2 2 3 4 5 2 6 Now we replace all values divisible by 3 with 123. Here are the revised contents of the vector: 1 2 2 123 4 5 2 123 Example 4Let's see another simple example: Output: myvector contains: 0 2 0 4 0 6 0 8 0 Next TopicC++ Algorithm |
C++ Algorithm swap_ranges() C++ Algorithm swap_ranges() exchanges the elements in the range [first1, last2) with the elements present in the range starting from first2. In short we can say that, swap_ranges() swaps the elements of two sequence, that is each element at a position in the first sequence...
5 min read
C++ Algorithm replace() 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. Syntax template <class ForwardIterator, class T> void...
4 min read
C++ Algorithm includes() C++ Algorithm includes() function returns true if every element from the sorted range [first2, last2) is found within the sorted range [first1, last1). It also returns true if [first2, last2) is empty. Elements are compared using operator < for the first version or using the given...
5 min read
C++ Algorithm Function adjacent_find() C++ Algorithm adjacent_find() function performs a search operation on the range [first, last] for the very first occurrence of two consecutive matching elements. If such elements are found then an iterator to the first element of the two is returned. Otherwise, the last...
1 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 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 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
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
C++ Algorithm is_sorted_until() C++ Algorithm is_sorted_until() function is used to find first unsorted element in the range. It means it returns an iterator to the first element in the range [first, last) which does not follow an ascending order. The elements are compared using operator < for...
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
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