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 |
algorithm remove_copy_if() function
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
algorithm equal() function
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
algorithm count() function
C++ Algorithm Function count() C++ Algorithm count()function accepts 'val' as argument and compares for the occurrence of element 'val' in the range. The number of occurrence of that element is returned. Syntax template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val); Parameter first: It is...
1 min read
algorithm for_each() function
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
algorithm stable_partition() function
C++ Algorithm stable_partition() C++ Algorithm stable_partition() function is used to classify the elements in the range [first, last), in such a way that all the elements for which pred returns true precede all those for which it returns false, where the relative order of elements is preserved. Note:...
4 min read
algorithm remove() function
C++ Algorithm remove() C++ Algorithm remove() function is used to eliminate all the elements that are equal to val from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the...
5 min read
algorithm inplace_merge() function
C++ Algorithm inplace_merge() C++ Algorithm inplace_merge() function is used to merge two consecutive sorted ranges [first, last) and [middle, last) into one sorted range [first, last). Elements are compared using operator < for the first version or using the given binary comparison function comp for the second version. Syntax default...
5 min read
algorithm replace_copy_if() function
C++ Algorithm replace_copy_if() C++ Algorithm replace_copy_if() function is used to make a copy of the range [first, last) to the range beginning at result, placing those for which pred returns true by new_value. It uses predicate pred instead of operator== to compare the elements. This function examines each...
8 min read
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 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
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