Sort (C++) - Usage

Usage

The sort function is included from the algorithm header of the C++ Standard Library, and carries three arguments: RandomAccessIterator first, RandomAccessIterator last, Compare comp. The third argument has default value - the "less-than" (<) operator to compare elements.

This code sample sorts a given array of integers (in ascending order) and prints it out. Pointers into the array serve as iterators.

#include #include int main { int array = { 23, 5, -10, 0, 0, 321, 1, 2, 99, 30 }; int elements = sizeof(array) / sizeof(array); std::sort(array, array + elements); for (int i = 0; i < elements; ++i) std::cout << array << ' '; }

The same functionality using vector container:

#include #include #include int main { std::vector vec; vec.push_back(10); vec.push_back(5); vec.push_back(100); std::sort(vec.begin, vec.end); for (int i = 0; i < vec.size; ++i) std::cout << vec << ' '; }

Read more about this topic:  Sort (C++)

Famous quotes containing the word usage:

    ...Often the accurate answer to a usage question begins, “It depends.” And what it depends on most often is where you are, who you are, who your listeners or readers are, and what your purpose in speaking or writing is.
    Kenneth G. Wilson (b. 1923)

    I am using it [the word ‘perceive’] here in such a way that to say of an object that it is perceived does not entail saying that it exists in any sense at all. And this is a perfectly correct and familiar usage of the word.
    —A.J. (Alfred Jules)

    Pythagoras, Locke, Socrates—but pages
    Might be filled up, as vainly as before,
    With the sad usage of all sorts of sages,
    Who in his life-time, each was deemed a bore!
    The loftiest minds outrun their tardy ages.
    George Gordon Noel Byron (1788–1824)