C++ Algorithm reverse()30 Aug 2024 | 3 min read C++ Algorithm reverse() function is used to reverse the order of the elements within a range [first, last). 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. Return valueNone ComplexityComplexity is linear in the range [first, last): swaps elements. Data racesThe object in the range [first, last) are modified. ExceptionsThis function throws an exception if either an element is swapped or an operation on iterator throws an exception. Note: The invalid parameters cause an undefined behavior.Example 1Let's see the simple example to reverse the given string: Output: Before Reverse : Hello Myself Nikita After Reverse : atikiN flesyM olleH Example 2Let'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 modified vector v1 with values reversed is: ( 9 8 7 6 5 4 3 2 1 0 ). Example 3Let's see another simple example: Output: Before: 10 11 12 13 14 15 16 17 Reverse only from index 5 to 7 in array: 10 11 12 13 14 17 16 15 Reverse full array: 7654 Example 4Let's see another simple example: Output: Original order : 1. George 2. John 3. Nik 4. Alice 5. Bob 6. Watson Reversing the order .... Reversed order : 1. Watson 2. Bob 3. Alice 4. Nik 5. John 6. George Next TopicC++ Algorithm |
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
C++ Algorithm _permutation () C++ Algorithm _permutation() function is used to reorder the elements in the range [first, last) into the lexicographically greater permutation. A permutation is specified as each of several possible ways in which a set or number of things can be ordered or...
6 min read
C++ Algorithm Function find_first_of() C++ Algorithm find_first_of() function compares the values stored in two containers i.e [first1, last1) and [first2, last2). If an element similar to the one in the range [first2, last2) is found in [first1, last1) then an iterator to that element is returned by...
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 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 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 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 iter_swap() C++ Algorithm iter_swap() exchanges the elements pointed to by two iterators a and b. Syntax template <class ForwardIterator1, class ForwardIterator2> void iter_swap (ForwardIterator1 a, ForwardIterator2 b); Parameter a: One of the forward iterator whose value is to be exchanged. b: Second of the forward iterator whose value is...
7 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 Functions all_of() C++ Algorithm all_of() function returns a true value if the value of 'pred' argument is true. The value should be true for all elements in the range [first, last]. Syntax template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies the...
1 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