C++ Algorithm reverse_copy()30 Aug 2024 | 4 min read C++ Algorithm reverse_copy() function is used to copy the elements from the range[first, last) to another range beginning at result in such a way that the elements in the range are in reverse order. SyntaxNote: BidirectionalIterator is an iterator which is used to access any elements of a container in both forward and backward direction.Parameterfirst: A bidirectional iterator pointing the position of the first element in the range in which the elements are being reversed. last: A forward iterator pointing the position one past the final element in the range in which the elements are being reversed. result: Output iterator pointing to the initial position of the range to which elements are being copied. Return valueThis function returns an output iterator pointing to the end of the copied range [first, last) to where the altered sequence of elements is being copied. ComplexityComplexity is linear in the range [first, last): performs an assignment to each element. Data racesThe object in the range [first, last) are accessed. The object in the range between result and returned value are modified. ExceptionsThis function throws an exception if either an element assignment or an operation on iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to demonstrate the use of reverse_copy(): Output: Before: 1 2 3 After: 3 2 1 Example 2Let's see another simple example to reverse the string: Output: Before Reverse: Hello Myself Nikita After Reverse: atikiN flesyM olleH Example 3Let's see another simple example to reverse the range of numbers: Output: The original vector v1 is: ( 0 1 2 3 4 5 6 7 8 9 ). The copy v2 of the reversed vector v1 is: ( 9 8 7 6 5 4 3 2 1 0 ). The original vector v1 remains unmodified as: ( 0 1 2 3 4 5 6 7 8 9 ). Example 4Let's see another simple example: Output: Original order : 1. George 2. John 3. Nikki 4. Alice 5. Bob 6. Watson Reversing the order ... Reversed order : 1. Watson 2. Bob 3. Alice 4. Nikki 5. John 6. George Next TopicC++ Algorithm |
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 Function equal() C++ Algorithm equal()function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching. The first range is from [first1,last1) and the second starts from first2. Syntax template<class InputIterator1, class...
2 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 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 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
C++ Algorithm is_sorted() C++ Algorithm is_sorted() function returns true if the elements in the range [first, last) are sorted into ascending order. The elements are compared using operator < for the first version, and comp for the second version. Syntax default (1) template <class ForwardIterator> ...
4 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 Functions none_of() C++ Algorithm none_of() function returns a true value if the value of 'pred' argument is false. The value should be false for all elements in the range [first, last). Syntax template <class InputIterator, class UnaryPredicate> bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first : It specifies...
2 min read
C++ Algorithm upper_bound() C++ Algorithm upper_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in the range [first, last) that is greater than to the specified value val. The first version uses operator < to compare...
5 min read
C++ Algorithm reverse() C++ Algorithm reverse() function is used to reverse the order of the elements within a range [first, last). Syntax template <class BidirectionalIterator> void reverse (BidirectionalIterator first, BidirectionalIterator last); Note: BidirectionalIterator is an iterator which is used to access any elements of a container in both forward and...
4 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