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:

    Girls who put out are tramps. Girls who don’t are ladies. This is, however, a rather archaic usage of the word. Should one of you boys happen upon a girl who doesn’t put out, do not jump to the conclusion that you have found a lady. What you have probably found is a lesbian.
    Fran Lebowitz (b. 1951)

    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)