⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sor_5132.htm

📁 ARM编辑、编译软件
💻 HTM
字号:
<HTML><HEAD><TITLE>Sorting Algorithms</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>&copy;Copyright 1996 Rogue Wave Software</P>
<H2>Sorting Algorithms</H2>
<P>There are two fundamental sorting algorithms provided by the standard library, described as follows:</P>
<PRE>void sort (RandomAccessIterator first, 
      RandomAccessIterator last [, Compare ] );

void stable_sort (RandomAccessIterator first, 
      RandomAccessIterator last [, Compare ] );
</PRE>
<P>The <SAMP>sort()</SAMP> algorithm is slightly faster, but it does not guarantee that equal elements in the original sequence will retain their relative orderings in the final result.  If order is important, then use the <SAMP>stable_sort()</SAMP> version.</P>
<P>Because these algorithms require random access iterators, they can be used only with vectors, deques, and ordinary C pointers.  Note, however, that the list container provides its own <SAMP>sort()</SAMP> member function.</P>
<P>The comparison operator can be explicitly provided when the default operator <SAMP>&#60;</SAMP> is not appropriate.  This is used in the example program to sort a list into descending, rather than ascending order.  An alternative technique for sorting an entire collection in the inverse direction is to describe the sequence using reverse iterators.</P>

<A HREF="sidebar.htm#sidebar72"><IMG SRC="images/note.gif" BORDER=0> <STRONG>More Sorts</STRONG></A>

<P>The following example program illustrates the <SAMP>sort()</SAMP> algorithm being applied to a <A HREF="../stdref/vec_0251.htm"><B><I>vector</I></B></A>, and the <SAMP>sort()</SAMP> algorithm with an explicit comparison operator being used with a <A HREF="../stdref/deq_4164.htm"><B><I>deque</I></B></A>.</P>
<PRE>void sort_example () 
   // illustrate the use of the sort algorithm
{
      // fill both a vector and a deque
      // with random integers
   vector&#60;int> aVec(15);
   deque&#60;int> aDec(15);
   generate (aVec.begin(), aVec.end(), randomValue);
   generate (aDec.begin(), aDec.end(), randomValue);
   
      // sort the vector ascending
   sort (aVec.begin(), aVec.end());
   
      // sort the deque descending
   sort (aDec.begin(), aDec.end(), greater&#60;int>() );

      // alternative way to sort descending
   sort (aVec.rbegin(), aVec.rend());
}
</PRE>

<HR>
<A HREF="ove_6146.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc.htm"><IMG SRC="images/toc.gif"></A> <A HREF="par_1934.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -