📅  最后修改于: 2020-10-17 06:26:23             🧑  作者: Mango
C++ STL algorithm.sort()函数用于将[first,last)范围内的元素按升序排序。
在第一个版本中使用运算符<比较元素,在第二个版本中使用comp进行比较。
default (1)
template
void sort (RandomAccessIterator first, RandomAccessIterator last);
custom (2)
template
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
first:随机访问迭代器,指向要排序范围内的第一个元素。
last:一个随机访问迭代器,它指向要排序范围内的最后一个last元素。
comp:用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。它遵循严格的弱排序来对元素进行排序。
没有
排序复杂度的平均值为N * log2(N),其中N =最后-首先。
[first,last)范围内的对象被修改。
如果元素比较,元素交换或迭代器上的任何操作抛出异常,则此函数将引发异常。
注意:无效的参数会导致未定义的行为。
让我们看一个简单的示例来演示sort()的用法:
#include
#include
#include
using namespace std;
int main()
{
vector 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
让我们看另一个简单的例子:
#include // std::cout
#include // std::sort
#include // std::vector
using namespace std;
bool myfunction (int i,int j) { return (i 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::iterator it=myvector.begin(); it!=myvector.end(); ++it)
cout << ' ' << *it;
cout << '\n';
return 0;
}
输出:
myvector contains: 12 26 32 33 45 53 71 80
让我们看另一个简单的例子:
#include
#include
#include
#include
using namespace std;
void print(const vector & v)
{
vector ::const_iterator i;
for(i = v.begin(); i != v.end(); i++)
{
cout << *i << " ";
}
cout << endl;
}
int main()
{
vector 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());
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#
让我们看另一个简单的例子:
#include
#include
#include
#include
using namespace std;
// return whether first element is greater than the second
bool userdefgreater(int elem1, int elem2)
{ return elem1 > elem2; }
int main()
{
vector vec1; // container
vector ::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 <());
cout <<"\nRe sorted (greater) vector vec1 data:\n";
for (Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout <<*Iter1<<" ";
cout <
输出:
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