<p>Summary: The C sort function is used to sort container elements. By default, it sorts in ascending order using the </p>
<p><img src="https://img.php.cn/upload/article/000/000/164/171205434266801.jpg" alt="Detailed explanation and example demonstration of C++sort function"></p>
<p><strong>C Detailed explanation and example demonstration of sort function</strong></p>
<p><strong>sort function overview</strong></p>
<p>sort function is a powerful function in the C Standard Template Library (STL) for sorting container elements. It arranges the elements in a container into ascending or descending order based on specified comparison rules. </p>
<p>The function is declared as follows: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:cpp;toolbar:false;'>template<typename Iter>
void sort(Iter first, Iter last);</pre><div class="contentsignin">Copy after login</div></div><p>Among them: </p><ul><li><strong>Iter</strong>: Iterator type pointing to the container element, which can be moved and accessed in the container element. </li><li><strong>first</strong>: Container start iterator, specifying the first element of the range of elements to be sorted. </li><li><strong>last</strong>: Container end iterator, specifying the elements after the last element of the range of elements to be sorted. </li></ul><p><strong>Custom comparison rules</strong></p><p>By default, the sort function uses the <code><</code> operator for comparison, which means it will Container elements are sorted in ascending order. If you wish to sort according to different rules, you can provide a custom comparison function: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:cpp;toolbar:false;'>bool compare(const Type1& a, const Type2& b)
{
// 自定义比较规则
}
// 在 sort 函数中使用自定义比较函数
sort(first, last, compare);</pre><div class="contentsignin">Copy after login</div></div><p><strong>Practical case</strong></p><p><strong>Example 1: Sorting an array of integers</strong></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:cpp;toolbar:false;'>#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[] = {5, 2, 7, 1, 3};
int len = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + len);
cout << "排序后的数组:";
for (int i = 0; i < len; i++)
{
cout << " " << arr[i];
}
cout << endl;
return 0;
}</pre><div class="contentsignin">Copy after login</div></div><p>Output: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>排序后的数组: 1 2 3 5 7</pre><div class="contentsignin">Copy after login</div></div><p><strong>Example 2: Sorting an array of strings</strong></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:cpp;toolbar:false;'>#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string arr[] = {"apple", "orange", "banana", "kiwi", "mango"};
int len = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + len);
cout << "排序后的数组:";
for (int i = 0; i < len; i++)
{
cout << " " << arr[i];
}
cout << endl;
return 0;
}</pre><div class="contentsignin">Copy after login</div></div><p>Output: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>排序后的数组: apple banana kiwi mango orange</pre><div class="contentsignin">Copy after login</div></div>
The above is the detailed content of Detailed explanation and example demonstration of C++sort function. For more information, please follow other related articles on the PHP Chinese website!