A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FSTL_Algorithms_Sorting%2Fsort</id>
		<title>C++ Tutorial/STL Algorithms Sorting/sort - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FSTL_Algorithms_Sorting%2Fsort"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/STL_Algorithms_Sorting/sort&amp;action=history"/>
		<updated>2026-04-17T17:47:55Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/STL_Algorithms_Sorting/sort&amp;diff=2733&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/STL_Algorithms_Sorting/sort&amp;diff=2733&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/STL_Algorithms_Sorting/sort&amp;diff=2734&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/STL_Algorithms_Sorting/sort&amp;diff=2734&amp;oldid=prev"/>
				<updated>2010-05-25T10:30:23Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Sort all element in an array==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
#include &amp;lt;iterator&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    int coll[] = { 5, 6, 2, 4, 1, 3 };&lt;br /&gt;
    // sort beginning with the second element&lt;br /&gt;
    sort (coll, coll+6);&lt;br /&gt;
    // print all elements&lt;br /&gt;
    copy (coll, coll+6,&lt;br /&gt;
          ostream_iterator&amp;lt;int&amp;gt;(cout,&amp;quot; &amp;quot;));&lt;br /&gt;
    cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1 2 3 4 5 6&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sort a vector and print out the sorted elements==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    vector&amp;lt;int&amp;gt; coll;&lt;br /&gt;
    vector&amp;lt;int&amp;gt;::iterator pos;&lt;br /&gt;
    // insert elements from 1 to 6 in arbitrary order&lt;br /&gt;
    coll.push_back(2);&lt;br /&gt;
    coll.push_back(5);&lt;br /&gt;
    coll.push_back(4);&lt;br /&gt;
    coll.push_back(1);&lt;br /&gt;
    coll.push_back(6);&lt;br /&gt;
    coll.push_back(3);&lt;br /&gt;
    // sort all elements&lt;br /&gt;
    sort (coll.begin(), coll.end());&lt;br /&gt;
    // print all elements&lt;br /&gt;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {&lt;br /&gt;
        cout &amp;lt;&amp;lt; *pos &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;1 2 3 4 5 6&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sort a vector into ascending order of id members==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cassert&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass : public binary_function&amp;lt;MyClass, MyClass, bool&amp;gt; {&lt;br /&gt;
public:&lt;br /&gt;
  int id;&lt;br /&gt;
  bool operator()(const MyClass&amp;amp; x, const MyClass&amp;amp; y) const {&lt;br /&gt;
     return x.id &amp;gt;= y.id;&lt;br /&gt;
  }&lt;br /&gt;
  friend ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; o, const MyClass&amp;amp; x) {&lt;br /&gt;
    o &amp;lt;&amp;lt; x.id;&lt;br /&gt;
    return o;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  vector&amp;lt;MyClass&amp;gt; vector1(100);&lt;br /&gt;
  for (int i = 0; i != 100; ++i)&lt;br /&gt;
    vector1[i].id = 100 - i - 1;&lt;br /&gt;
  sort(vector1.begin(), vector1.end(), not2(MyClass()));&lt;br /&gt;
  for (int k = 0; k != 100; ++k)&lt;br /&gt;
    cout &amp;lt;&amp;lt; vector1[k].id &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29&lt;br /&gt;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56&lt;br /&gt;
 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8&lt;br /&gt;
3 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 &amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sort elements in deque==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;deque&amp;gt;&lt;br /&gt;
#include &amp;lt;list&amp;gt;&lt;br /&gt;
#include &amp;lt;set&amp;gt;&lt;br /&gt;
#include &amp;lt;map&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;iterator&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
#include &amp;lt;numeric&amp;gt;&lt;br /&gt;
/* PRINT_ELEMENTS()&lt;br /&gt;
 * - prints optional C-string optcstr followed by&lt;br /&gt;
 * - all elements of the collection coll&lt;br /&gt;
 * - separated by spaces&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
inline void PRINT_ELEMENTS (const T&amp;amp; coll, const char* optcstr=&amp;quot;&amp;quot;)&lt;br /&gt;
{&lt;br /&gt;
    typename T::const_iterator pos;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; optcstr;&lt;br /&gt;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; *pos &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&lt;br /&gt;
/* INSERT_ELEMENTS (collection, first, last)&lt;br /&gt;
 * - fill values from first to last into the collection&lt;br /&gt;
 * - NOTE: NO half-open range&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
inline void INSERT_ELEMENTS (T&amp;amp; coll, int first, int last)&lt;br /&gt;
{&lt;br /&gt;
    for (int i=first; i&amp;lt;=last; ++i) {&lt;br /&gt;
        coll.insert(coll.end(),i);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    deque&amp;lt;int&amp;gt; coll;&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,9);&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,9);&lt;br /&gt;
    PRINT_ELEMENTS(coll,&amp;quot;on entry: &amp;quot;);&lt;br /&gt;
    // sort elements&lt;br /&gt;
    sort (coll.begin(), coll.end());&lt;br /&gt;
    PRINT_ELEMENTS(coll,&amp;quot;sorted:   &amp;quot;);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;on entry: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9&lt;br /&gt;
sorted:   1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sort elements reversely with custom function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;deque&amp;gt;&lt;br /&gt;
#include &amp;lt;list&amp;gt;&lt;br /&gt;
#include &amp;lt;set&amp;gt;&lt;br /&gt;
#include &amp;lt;map&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;iterator&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
#include &amp;lt;numeric&amp;gt;&lt;br /&gt;
/* PRINT_ELEMENTS()&lt;br /&gt;
 * - prints optional C-string optcstr followed by&lt;br /&gt;
 * - all elements of the collection coll&lt;br /&gt;
 * - separated by spaces&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
inline void PRINT_ELEMENTS (const T&amp;amp; coll, const char* optcstr=&amp;quot;&amp;quot;)&lt;br /&gt;
{&lt;br /&gt;
    typename T::const_iterator pos;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; optcstr;&lt;br /&gt;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; *pos &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&lt;br /&gt;
/* INSERT_ELEMENTS (collection, first, last)&lt;br /&gt;
 * - fill values from first to last into the collection&lt;br /&gt;
 * - NOTE: NO half-open range&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
inline void INSERT_ELEMENTS (T&amp;amp; coll, int first, int last)&lt;br /&gt;
{&lt;br /&gt;
    for (int i=first; i&amp;lt;=last; ++i) {&lt;br /&gt;
        coll.insert(coll.end(),i);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    deque&amp;lt;int&amp;gt; coll;&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,9);&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,9);&lt;br /&gt;
    PRINT_ELEMENTS(coll,&amp;quot;on entry: &amp;quot;);&lt;br /&gt;
    // sorted reverse&lt;br /&gt;
    sort (coll.begin(), coll.end(),    // range&lt;br /&gt;
          greater&amp;lt;int&amp;gt;());             // sorting criterion&lt;br /&gt;
    PRINT_ELEMENTS(coll,&amp;quot;sorted &amp;gt;: &amp;quot;);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;on entry: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9&lt;br /&gt;
sorted &amp;gt;: 9 9 8 8 7 7 6 6 5 5 4 4 3 3 2 2 1 1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Sort part of the elements in an array==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
#include &amp;lt;iterator&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    int coll[] = { 5, 6, 2, 4, 1, 3 };&lt;br /&gt;
    // sort beginning with the second element&lt;br /&gt;
    sort (coll+3, coll+6);&lt;br /&gt;
    // print all elements&lt;br /&gt;
    copy (coll, coll+6,ostream_iterator&amp;lt;int&amp;gt;(cout,&amp;quot; &amp;quot;));&lt;br /&gt;
    cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;5 6 2 1 3 4&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use custom function and sort to sort strings by length==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;/* The following code example is taken from the book&lt;br /&gt;
 * &amp;quot;The C++ Standard Library - A Tutorial and Reference&amp;quot;&lt;br /&gt;
 * by Nicolai M. Josuttis, Addison-Wesley, 1999&lt;br /&gt;
 *&lt;br /&gt;
 * (C) Copyright Nicolai M. Josuttis 1999.&lt;br /&gt;
 * Permission to copy, use, modify, sell and distribute this software&lt;br /&gt;
 * is granted provided this copyright notice appears in all copies.&lt;br /&gt;
 * This software is provided &amp;quot;as is&amp;quot; without express or implied&lt;br /&gt;
 * warranty, and with no claim as to its suitability for any purpose.&lt;br /&gt;
 */&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;deque&amp;gt;&lt;br /&gt;
#include &amp;lt;list&amp;gt;&lt;br /&gt;
#include &amp;lt;set&amp;gt;&lt;br /&gt;
#include &amp;lt;map&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;iterator&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
#include &amp;lt;numeric&amp;gt;&lt;br /&gt;
/* PRINT_ELEMENTS()&lt;br /&gt;
 * - prints optional C-string optcstr followed by&lt;br /&gt;
 * - all elements of the collection coll&lt;br /&gt;
 * - separated by spaces&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
inline void PRINT_ELEMENTS (const T&amp;amp; coll, const char* optcstr=&amp;quot;&amp;quot;)&lt;br /&gt;
{&lt;br /&gt;
    typename T::const_iterator pos;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; optcstr;&lt;br /&gt;
    for (pos=coll.begin(); pos!=coll.end(); ++pos) {&lt;br /&gt;
        std::cout &amp;lt;&amp;lt; *pos &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&lt;br /&gt;
/* INSERT_ELEMENTS (collection, first, last)&lt;br /&gt;
 * - fill values from first to last into the collection&lt;br /&gt;
 * - NOTE: NO half-open range&lt;br /&gt;
 */&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
inline void INSERT_ELEMENTS (T&amp;amp; coll, int first, int last)&lt;br /&gt;
{&lt;br /&gt;
    for (int i=first; i&amp;lt;=last; ++i) {&lt;br /&gt;
        coll.insert(coll.end(),i);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
using namespace std;&lt;br /&gt;
bool lessLength (const string&amp;amp; s1, const string&amp;amp; s2)&lt;br /&gt;
{&lt;br /&gt;
    return s1.length() &amp;lt; s2.length();&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    vector&amp;lt;string&amp;gt; coll1;&lt;br /&gt;
    vector&amp;lt;string&amp;gt; coll2;&lt;br /&gt;
    // fill both collections with the same elements&lt;br /&gt;
    coll1.push_back (&amp;quot;1xxx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;2x&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;3x&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;4x&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;5xx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;6xxxx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;7xx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;8xxx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;9xx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;10xxx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;11&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;12&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;13&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;14xx&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;15&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;16&amp;quot;);&lt;br /&gt;
    coll1.push_back (&amp;quot;17&amp;quot;);&lt;br /&gt;
    coll2 = coll1;&lt;br /&gt;
    PRINT_ELEMENTS(coll1,&amp;quot;on entry:\n &amp;quot;);&lt;br /&gt;
    // sort (according to the length of the strings)&lt;br /&gt;
    sort (coll1.begin(), coll1.end(),           // range&lt;br /&gt;
          lessLength);                          // criterion&lt;br /&gt;
    stable_sort (coll2.begin(), coll2.end(),    // range&lt;br /&gt;
                 lessLength);                   // criterion&lt;br /&gt;
    PRINT_ELEMENTS(coll1,&amp;quot;\nwith sort():\n &amp;quot;);&lt;br /&gt;
    PRINT_ELEMENTS(coll2,&amp;quot;\nwith stable_sort():\n &amp;quot;);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;on entry:&lt;br /&gt;
 1xxx 2x 3x 4x 5xx 6xxxx 7xx 8xxx 9xx 10xxx 11 12 13 14xx 15 16 17&lt;br /&gt;
with sort():&lt;br /&gt;
 17 2x 3x 4x 16 15 13 12 11 9xx 7xx 5xx 8xxx 14xx 1xxx 10xxx 6xxxx&lt;br /&gt;
with stable_sort():&lt;br /&gt;
 2x 3x 4x 11 12 13 15 16 17 5xx 7xx 9xx 1xxx 8xxx 14xx 6xxxx 10xxx&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using an in-place generic sort algorithm==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;cassert&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main() {&lt;br /&gt;
  int a[1000];&lt;br /&gt;
  int i;&lt;br /&gt;
  &lt;br /&gt;
  for (i = 0; i &amp;lt; 1000; ++i) &lt;br /&gt;
    a[i] = 1000 - i - 1;&lt;br /&gt;
  sort(&amp;amp;a[0], &amp;amp;a[1000]);&lt;br /&gt;
  for (i = 0; i &amp;lt; 1000; ++i) &lt;br /&gt;
    assert (a[i] == i);&lt;br /&gt;
  &lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Using the generic sort algorithm with a binary predicate: greater==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;cassert&amp;gt;&lt;br /&gt;
#include &amp;lt;functional&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main() {&lt;br /&gt;
  int a[100];&lt;br /&gt;
  int i;&lt;br /&gt;
  for (i = 0; i &amp;lt; 100; ++i)&lt;br /&gt;
    a[i] = i;&lt;br /&gt;
  random_shuffle(&amp;amp;a[0], &amp;amp;a[100]);&lt;br /&gt;
  for (i = 0; i &amp;lt; 100; ++i)&lt;br /&gt;
    cout &amp;lt;&amp;lt;  a[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
  cout &amp;lt;&amp;lt;&amp;quot;\n\n\n\n&amp;quot;;&lt;br /&gt;
  // Sort into descending order:&lt;br /&gt;
  sort(&amp;amp;a[0], &amp;amp;a[100], greater&amp;lt;int&amp;gt;());&lt;br /&gt;
&lt;br /&gt;
  for (i = 0; i &amp;lt; 100; ++i)&lt;br /&gt;
    cout &amp;lt;&amp;lt;  a[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;12 1 9 98 96 27 58 82 86 90 18 62 32 40 71 51 91 41 94 17 8 47 64 66 65 7 6 76 5&lt;br /&gt;
 99 77 81 54 35 56 39 25 3 87 16 61 68 14 13 24 55 97 19 20 59 75 33 21 28 78 15&lt;br /&gt;
 50 34 36 44 83 38 46 60 84 95 57 22 37 23 70 89 31 79 73 92 11 2 88 42 30 52 72&lt;br /&gt;
 53 67 29 85 43 74 69 45 26 93 10 48 80 0 63 49 4&lt;br /&gt;
&lt;br /&gt;
99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73&lt;br /&gt;
 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 4&lt;br /&gt;
6 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20&lt;br /&gt;
19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>