版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hf19931101/article/details/82719494
STL中更有很多函数十分常用,其中排序是使用最高频的函数之一,以下针对C++11的排序进行总结。
1. 排序(sort)
1.1 std::sort
1.1.2 常用形式
//用 operator< 比较元素。
template< class RandomIt >
void sort( RandomIt first, RandomIt last );
//用给定的二元比较函数 comp 比较元素。
template< class RandomIt, class Compare >
void sort( RandomIt first, RandomIt last, Compare comp );
以升序排序范围 [first, last) 中的元素。不保证维持相等元素的顺序。
时间复杂度是平均 O(N·log(N))
次比较,其中 N = std::distance(first, last)
。
1.1.2 使用示例
1. 内置类型
//降序
bool Compare(const int& a, const int&b) {
return a > b;
}
//vector
vector<int> v1{ 1,3,2,9,7,6,5,8,4 };
std::sort(v1.begin(), v1.end()); //默认升序
std::sort(v1.begin(), v1.end(), Compare); //降序
//deque
deque<int> d1{ 1,3,2,9,7,6,5,8,4 };
std::sort(d1.begin(), d1.end()); //默认升序
std::sort(d1.begin(), d1.end(), Compare); //降序
//array
array<int, 10> a1{ 1,3,2,9,7,6,10,5,8,4 };
std::sort(a1.begin(), a1.end()); //默认升序
std::sort(a1.begin(), a1.end(), Compare); //降序
list和forward_list需要使用自己的sort方法。
2.自定义类型
struct Node {
int a;
int b;
Node() {}
Node(int a, int b) {
this->a = a;
this->b = b;
}
//使用std::sort()需要要重载<操作符,使之满足严格弱序
bool operator< (const Node& right) const {
if (a == right.a)
{
return b < right.b;
}
else {
return a < right.a;
}
}
};
//按node.a升序,node.b降序
bool Compare2(const Node& left, const Node& right) {
if (left.a == right.a)
{
return left.b > right.b;
}
else {
return left.a < right.a;
}
}
//vector
vector<Node> v2;
v2.push_back(Node(0, 1));
v2.push_back(Node(0, 3));
v2.push_back(Node(0, 2));
v2.push_back(Node(1, 2));
v2.push_back(Node(1, 1));
v2.push_back(Node(1, 3));
std::sort(v2.begin(), v2.end()); //node.a升序,node.b升序
std::sort(v2.begin(), v2.end(), Compare2); //node.a升序,node.b降序
//deque
vector<Node> d2;
d2.push_back(Node(0, 1));
d2.push_back(Node(0, 3));
d2.push_back(Node(0, 2));
d2.push_back(Node(1, 2));
d2.push_back(Node(1, 1));
d2.push_back(Node(1, 3));
std::sort(d2.begin(), d2.end()); //node.a升序,node.b升序
std::sort(d2.begin(), d2.end(), Compare2); //node.a升序,node.b降序
//array
array<Node, 6> a2 ; //必须有无参构造函数
a2[0]=(Node(0, 1));
a2[1] = (Node(0, 3));
a2[2] = (Node(0, 2));
a2[3] = (Node(1, 2));
a2[4] = (Node(1, 1));
a2[5] = (Node(1, 3));
std::sort(a2.begin(), a2.end()); //node.a升序,node.b升序
std::sort(a2.begin(), a2.end(), Compare2); //node.a升序,node.b降序
1.2 list::sort
std::sort 要求随机访问迭代器且不能用于 list 。此函数与 std::sort 的区别在于,它不要求 list 的元素类型可交换,保留所有迭代器的值,并进行稳定排序。
1.2.1 常用形式
void sort();
template< class Compare >
void sort( Compare comp );
大约 N log N
次比较,其中 N 是表中的元素数。
1.2.2 使用示例
struct Node {
int a;
int b;
Node() {}
Node(int a, int b) {
this->a = a;
this->b = b;
}
//使用list::sort()需要要重载<操作符,使之满足严格弱序
bool operator< (const Node& right) const {
if (a == right.a)
{
return b < right.b;
}
else {
return a < right.a;
}
}
};
bool Compare(const int& a, const int&b) {
return a > b;
}
//按node.a升序,node.b降序
bool Compare2(const Node& left, const Node& right) {
if (left.a == right.a)
{
return left.b > right.b;
}
else {
return left.a < right.a;
}
}
list<int> l1{ 1,3,2,9,7,6,5,8,4 };
l1.sort(); //默认升序
l1.sort(Compare); //降序
list<Node> l2;
l2.push_back(Node(0, 2));
l2.push_back(Node(0, 1));
l2.push_back(Node(0, 3));
l2.push_back(Node(1, 3));
l2.push_back(Node(1, 1));
l2.push_back(Node(1, 2));
l2.sort(); //默认node.a升序,node.b升序
l2.sort(Compare2); //node.a升序,node.b降序
1.3 forward_list::sort
std::sort 要求随机访问迭代器且不能用于 forward_list 。此函数与 std::sort 的区别在于,它不要求 forward_list 的元素类型可交换,保留所有迭代器的值,并进行稳定排序。
1.3.1 常用形式
//C++11起
void sort();
//C++11起
template< class Compare >
void sort( Compare comp );
大约要进行 N log N
次比较,其中 N 是表中的元素数。
1.3.2 使用示例
struct Node {
int a;
int b;
Node() {}
Node(int a, int b) {
this->a = a;
this->b = b;
}
//使用list::sort()需要要重载<操作符,使之满足严格弱序
bool operator< (const Node& right) const {
if (a == right.a)
{
return b < right.b;
}
else {
return a < right.a;
}
}
};
bool Compare(const int& a, const int&b) {
return a > b;
}
//按node.a升序,node.b降序
bool Compare2(const Node& left, const Node& right) {
if (left.a == right.a)
{
return left.b > right.b;
}
else {
return left.a < right.a;
}
}
forward_list<int> l1{ 1,3,2,9,7,6,5,8,4 };
l1.sort(); //默认升序
l1.sort(Compare); //降序
forward_list<Node> l2;
l2.push_front(Node(0, 2));
l2.push_front(Node(0, 1));
l2.push_front(Node(0, 3));
l2.push_front(Node(1, 3));
l2.push_front(Node(1, 1));
l2.push_front(Node(1, 2));
l2.sort(); //默认node.a升序,node.b升序
l2.sort(Compare2); //node.a升序,node.b降序