C++ algorithm reverse() function
Example
Reverse the order of elements in a vector:
vector<int> numbers = {1, 3, 5, 7, 2, 9};
reverse(numbers.begin(), numbers.end());
for (int number : numbers) {
cout << number << " ";
}
Try it Yourself »
Definition and Usage
The reverse()
function reverses the order of elements in a data range.
The range of data is specified by iterators.
Tip: To avoid modifying the data range and create a new data range instead, you can use the reverse_copy()
function.
Syntax
reverse(iterator start, iterator end);
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range. |
end | Required. An iterator pointing to the end of the data range. Elements up to this position will be included, but the element at this position will not be. |
Related Pages
Read more about data structures in our Data Structures Tutorial.
Read more about iterators in our Iterators Tutorial.
Read more about algorithms in our Algorithms Tutorial.