C++算法sort()函數用於將[first, last)範圍內的元素按升序排序。
第一個版本使用運算符比較元素,第二個版本使用 comp 進行比較。
用法
default (1)
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
custom (2)
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
參數
first:一個隨機訪問迭代器,指向要排序的範圍中的第一個元素。
last:一個隨機訪問迭代器,指向要排序的範圍中過去的最後一個元素。
comp: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。
返回值
空
複雜度
排序複雜度的平均值是 N*log2(N),其中 N = last - first。
數據競爭
範圍 [first, last) 中的對象被修改。
異常
如果任何元素比較、元素交換或迭代器上的操作引發異常,則此函數將引發異常。
注意:無效的參數會導致未定義的行為。
例子1
讓我們看一個簡單的例子來演示 sort() 的使用:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int> v = {3, 1, 4, 2, 5};
cout<<"Before sorting:";
for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});
sort(v.begin(), v.end());
cout<<"\nAfter sorting: ";
for_each(v.begin(), v.end(), [](int x) {
cout << x << " ";
});
return 0;
}
輸出:
Before sorting:3 1 4 2 5 After sorting: 1 2 3 4 5
例子2
讓我們看另一個簡單的例子:
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
using namespace std;
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;
int main () {
int myints[] = {32,71,12,45,26,80,53,33};
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
cout << "myvector contains:";
for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
輸出:
myvector contains:12 26 32 33 45 53 71 80
例子3
讓我們看另一個簡單的例子:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void print(const vector <std::string>& v)
{
vector <string>::const_iterator i;
for(i = v.begin(); i != v.end(); i++)
{
cout << *i << " ";
}
cout << endl;
}
int main()
{
vector <string> v;
// Push functional programming languages
v.push_back("Lisp");
v.push_back("C#");
v.push_back("Java");
v.push_back("Python");
v.push_back("C++");
v.push_back("Pascal");
v.push_back("Sql");
// sort without predicate
sort(v.begin(), v.end());
cout << "Sorted list of functional programming languages - " << endl;
print(v);
// sort with predicate
sort(v.begin(), v.end(), std::greater<std::string>());
cout << "Reverse Sorted list of functional programming languages - " << endl;
print(v);
}
輸出:
Sorted list of functional programming languages - C# C++ Java Lisp Pascal Python Sql Reverse Sorted list of functional programming languages - Sql Python Pascal Lisp Java C++ C#
示例 4
讓我們看另一個簡單的例子:
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
// return whether first element is greater than the second
bool userdefgreater(int elem1, int elem2)
{ return elem1 > elem2; }
int main()
{
vector <int> vec1; // container
vector <int>::iterator Iter1; // iterator
int k;
for (k = 0; k <= 15; k++)
vec1.push_back(k);
random_shuffle(vec1.begin(), vec1.end());
cout <<"Original random shuffle vector vec1 data:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
sort(vec1.begin(), vec1.end());
cout <<"\nSorted vector vec1 data:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
// to sort in descending order, specify binary predicate
sort(vec1.begin(), vec1.end(), greater<int>());
cout <<"\nRe sorted (greater) vector vec1 data:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
// a user-defined binary predicate can also be used
sort(vec1.begin(), vec1.end(), userdefgreater);
cout <<"\nUser defined re sorted vector vec1 data:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <<endl;
return 0;
}
輸出:
Original random shuffle vector vec1 data: 4 10 11 15 14 5 13 1 6 9 3 7 8 2 0 12 Sorted vector vec1 data: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Re sorted (greater) vector vec1 data: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 User defined re sorted vector vec1 data: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
相關用法
- C++ Algorithm sort_heap()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm set_difference()用法及代碼示例
- C++ Algorithm stable_sort()用法及代碼示例
- C++ Algorithm stable_partition()用法及代碼示例
- C++ Algorithm set_symmetric_difference()用法及代碼示例
- C++ Algorithm swap_ranges()用法及代碼示例
- C++ Algorithm shuffle()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm sort()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。