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%2FSTL_Algorithms_Sorting%2Fnth_element</id>
		<title>C++/STL Algorithms Sorting/nth element - История изменений</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%2FSTL_Algorithms_Sorting%2Fnth_element"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/STL_Algorithms_Sorting/nth_element&amp;action=history"/>
		<updated>2026-04-19T01:20:13Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/STL_Algorithms_Sorting/nth_element&amp;diff=1825&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/STL_Algorithms_Sorting/nth_element&amp;diff=1825&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</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/STL_Algorithms_Sorting/nth_element&amp;diff=1826&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/STL_Algorithms_Sorting/nth_element&amp;diff=1826&amp;oldid=prev"/>
				<updated>2010-05-25T10:27:58Z</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;==Finding the Median==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
template &amp;lt;class T&amp;gt;&lt;br /&gt;
void print(T&amp;amp; c){&lt;br /&gt;
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){&lt;br /&gt;
      std::cout &amp;lt;&amp;lt; *i &amp;lt;&amp;lt; endl;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
int main( ) {&lt;br /&gt;
   const int len = 9;&lt;br /&gt;
   const int a[len] = { 5, 6, 4, 3,2, 6, 7, 9, 3 };&lt;br /&gt;
   vector&amp;lt;int&amp;gt; v( a, a+len );&lt;br /&gt;
   print( v );&lt;br /&gt;
   // midpoint is the median&lt;br /&gt;
   nth_element( v.begin(), v.begin()+len/2,v.end() );&lt;br /&gt;
   print( v );&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;\nMedian     salary: &amp;quot; &amp;lt;&amp;lt; v[len/2];&lt;br /&gt;
   // display sorted v for clarity&lt;br /&gt;
   sort( v.begin(), v.end() );&lt;br /&gt;
   print( v  );&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Illustrating the generic nth_element algorithm==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cassert&amp;gt;&lt;br /&gt;
#include &amp;lt;algorithm&amp;gt;&lt;br /&gt;
#include &amp;lt;vector&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; v(7);&lt;br /&gt;
  v[0] = 25; &lt;br /&gt;
  v[1] = 7; &lt;br /&gt;
  v[2] = 9;&lt;br /&gt;
  v[3] = 2; &lt;br /&gt;
  v[4] = 0; &lt;br /&gt;
  v[5] = 5; &lt;br /&gt;
  v[6] = 21;&lt;br /&gt;
  const int N = 4;&lt;br /&gt;
  // Use nth_element to place the Nth smallest &lt;br /&gt;
  // element in v in the Nth position, v.begin() + N:&lt;br /&gt;
  nth_element(v.begin(), v.begin() + N, v.end());&lt;br /&gt;
  &lt;br /&gt;
  for (int i = 0; i &amp;lt; N; ++i)&lt;br /&gt;
    assert (v[N] &amp;gt;= v[i]);&lt;br /&gt;
  &lt;br /&gt;
  for (int i = N + 1; i &amp;lt; 7; ++i)&lt;br /&gt;
     cout &amp;lt;&amp;lt; v[N];&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
/* &lt;br /&gt;
99&lt;br /&gt;
 */&lt;br /&gt;
        &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use nth_element to extract the four highest elements==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
    &lt;br /&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,3,7);&lt;br /&gt;
    INSERT_ELEMENTS(coll,2,6);&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,5);&lt;br /&gt;
    PRINT_ELEMENTS(coll);&lt;br /&gt;
    // extract the four highest elements&lt;br /&gt;
    nth_element (coll.begin(),     // beginning of range&lt;br /&gt;
                 coll.end()-4,     // element that should be sorted correctly&lt;br /&gt;
                 coll.end());      // end of range&lt;br /&gt;
    // print them&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;the four highest elements are: &amp;quot;;&lt;br /&gt;
    copy (coll.end()-4, coll.end(),&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;
}&lt;br /&gt;
/* &lt;br /&gt;
3 4 5 6 7 2 3 4 5 6 1 2 3 4 5&lt;br /&gt;
the four highest elements are: 5 6 7 6&lt;br /&gt;
 */        &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use nth_element to extract the four lowest elements==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
    &lt;br /&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,3,7);&lt;br /&gt;
    INSERT_ELEMENTS(coll,2,6);&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,5);&lt;br /&gt;
    PRINT_ELEMENTS(coll);&lt;br /&gt;
    // extract the four lowest elements&lt;br /&gt;
    nth_element (coll.begin(),     // beginning of range&lt;br /&gt;
                 coll.begin()+3,   // element that should be sorted correctly&lt;br /&gt;
                 coll.end());      // end of range&lt;br /&gt;
    // print them&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;the four lowest elements are:  &amp;quot;;&lt;br /&gt;
    copy (coll.begin(), coll.begin()+4,&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;
}&lt;br /&gt;
 /* &lt;br /&gt;
3 4 5 6 7 2 3 4 5 6 1 2 3 4 5&lt;br /&gt;
the four lowest elements are:  2 1 2 3&lt;br /&gt;
 */       &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use nth_element with custom function to extract the four highest elements==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
 &lt;br /&gt;
    &lt;br /&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,3,7);&lt;br /&gt;
    INSERT_ELEMENTS(coll,2,6);&lt;br /&gt;
    INSERT_ELEMENTS(coll,1,5);&lt;br /&gt;
    PRINT_ELEMENTS(coll);&lt;br /&gt;
    // extract the four highest elements (second version)&lt;br /&gt;
    nth_element (coll.begin(),     // beginning of range&lt;br /&gt;
                 coll.begin()+3,   // element that should be sorted correctly&lt;br /&gt;
                 coll.end(),       // end of range&lt;br /&gt;
                 greater&amp;lt;int&amp;gt;());  // sorting criterion&lt;br /&gt;
    // print them&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;the four highest elements are: &amp;quot;;&lt;br /&gt;
    copy (coll.begin(), coll.begin()+4,&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;
}&lt;br /&gt;
 /* &lt;br /&gt;
3 4 5 6 7 2 3 4 5 6 1 2 3 4 5&lt;br /&gt;
the four highest elements are: 6 7 6 5&lt;br /&gt;
 */       &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>