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:

    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)

    ...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)

    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)