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

📄 sort.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <meta name="generator" content=  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">  <title>sort</title>  <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table>  <tr>  <td>  <div class="body-content">  <div class="header-box">    <a href="../index.html">cppreference.com</a> &gt; <a href=    "index.html">C++ Algorithms</a> &gt; <a href="sort.html">sort</a>  </div>  <div class="name-format">    sort  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  void sort( iterator start, iterator end );  void sort( iterator start, iterator end, StrictWeakOrdering cmp );</pre>  <p>The sort() algorithm sorts the elements in the range  [<em>start</em>,<em>end</em>) into ascending order. If two elements  are equal, there is no guarantee what order they will be in.</p>  <p>If the strict weak ordering function object <em>cmp</em> is given,  then it will be used to compare two objects instead of the &lt;  operator.</p>  <p>The algorithm behind sort() is the <em>introsort</em> algorithm.  sort() runs in O(N log(N)) time (average and worst case) which is  faster than polynomial time but slower than <a href=  "../complexity.html">linear time</a>.</p>  <div class="related-examples-format">    Example code:  </div>  <div class="related-examples">    <p>For example, the following code sorts a vector of integers into    ascending order:</p>    <pre class="example-code"> vector&lt;int&gt; v; v.push_back( 23 ); v.push_back( -1 ); v.push_back( 9999 ); v.push_back( 0 ); v.push_back( 4 );               cout &lt;&lt; &quot;Before sorting: &quot;; for( unsigned int i = 0; i &lt; v.size(); i++ ) {   cout &lt;&lt; v[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;             sort( v.begin(), v.end() );             cout &lt;&lt; &quot;After sorting: &quot;; for( unsigned int i = 0; i &lt; v.size(); i++ ) {   cout &lt;&lt; v[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>    <p>When run, the above code displays this output:</p>    <pre class="example-code"> Before sorting: 23 -1 9999 0 4 After sorting: -1 0 4 23 9999          </pre>    <p>Alternatively, the following code uses the sort() function to    sort a normal array of integers, and displays the same output as    the previous example:</p>    <pre class="example-code"> int array[] = { 23, -1, 9999, 0, 4 }; unsigned int array_size = 5;            cout &lt;&lt; &quot;Before sorting: &quot;; for( unsigned int i = 0; i &lt; array_size; i++ ) {   cout &lt;&lt; array[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;             sort( array, array + array_size );              cout &lt;&lt; &quot;After sorting: &quot;; for( unsigned int i = 0; i &lt; array_size; i++ ) {   cout &lt;&lt; array[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>    <p>This next example shows how to use sort() with a user-specified    comparison function. The function <strong>cmp</strong> is defined    to do the opposite of the &lt; operator. When sort() is called    with <strong>cmp</strong> used as the comparison function, the    result is a list sorted in descending, rather than ascending,    order:</p>    <pre class="example-code"> bool cmp( int a, int b ) {   return a &gt; b; }               ...             vector&lt;int&gt; v; for( int i = 0; i &lt; 10; i++ ) {   v.push_back(i); }               cout &lt;&lt; &quot;Before: &quot;; for( int i = 0; i &lt; 10; i++ ) {   cout &lt;&lt; v[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;             sort( v.begin(), v.end(), cmp );                cout &lt;&lt; &quot;After: &quot;; for( int i = 0; i &lt; 10; i++ ) {   cout &lt;&lt; v[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>  </div>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="binary_search.html">binary_search</a><br>    <a href="is_sorted.html">is_sorted</a><br>    <a href="merge.html">merge</a><br>    <a href="partial_sort.html">partial_sort</a><br>    <a href="partial_sort_copy.html">partial_sort_copy</a><br>    <a href="stable_sort.html">stable_sort</a><br>    (Other Standard C Functions) <a href=    "../stdother/qsort.html">qsort</a>  </div>  </div>  </td>    </tr>  </table></body></html>

⌨️ 快捷键说明

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