C++ algorithmreverse() 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
Thereverse()
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 thereverse_copy()
function.
Syntax
reverse(iteratorstart, iteratorend);
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 ourData Structures Tutorial.
Read more about iterators in ourIterators Tutorial.
Read more about algorithms in ourAlgorithms Tutorial.