C++ algorithm sort() function
Example
Sort the elements in a vector:
vector<int> numbers = {1, 3, 5, 7, 2, 9};
sort(numbers.begin(), numbers.end());
for (int number : numbers) {
cout << number << " ";
}
Try it Yourself »
Definition and Usage
The sort()
function sorts the elements of a data range in ascending order.
The range of data is specified by iterators.
Syntax
sort(iterator start, iterator end);
Parameter Values
Parameter | Description |
---|---|
start | Required. An iterator pointing to the start of the data range to be sorted. |
end | Required. An iterator pointing to the end of the data range to be sorted. 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.