std::sort是C++标准库中的一个函数,用于对容器中的元素进行排序。它可以按照默认的升序方式对元素进行排序,也可以指定自定义的比较函数来实现降序排序等其他排序方式。
函数介绍
std::sort
函数位于<algorithm>
头文件中,其原型如下:
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
其中,first
和last
表示待排序序列的起始和结束迭代器,comp
是一个可选的比较函数,用于指定排序规则。如果不提供比较函数,则默认按照升序排序。
使用场景
对数组进行排序
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 现在 arr 数组已经按照升序排列为 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}
对 vector 进行排序
std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(v.begin(), v.end());
// 现在 v 向量已经按照升序排列为 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}
对字符串进行排序
std::string str = "hello world";
std::sort(str.begin(), str.end());
// 现在 str 字符串已经按照字母表顺序排列为 "dehllloorw"
对 pair 进行排序
std::pair<int, std::string> p1 = {3, "apple"};
std::pair<int, std::string> p2 = {1, "banana"};
std::pair<int, std::string> p3 = {2, "orange"};
std::vector<std::pair<int, std::string>> vec = {p1, p2, p3};
std::sort(vec.begin(), vec.end());
// 现在 vec 向量已经按照第一个元素升序排列为 {{1, "banana"}, {2, "orange"}, {3, "apple"}}
完整示例
例子1: 对数组进行正向排序
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n);
cout << "Sorted array is: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
输出结果为:Sorted array is: 1 1 2 3 3 4 5 5 5 6 9
例子2: 对 vector 进行正向排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(v.begin(), v.end());
cout << "Sorted vector is: ";
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}
输出结果为:Sorted vector is: 1 1 2 3 3 4 5 5 5 6 9
例子3: 对字符串进行正向排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string str = "hello world";
sort(str.begin(), str.end());
cout << "Sorted string is: " << str << endl;
return 0;
}
输出结果为:Sorted string is: dehllloorw
例子4: 对 pair 进行正向排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool compare(const pair<int, string>& a, const pair<int, string>& b) {
return a.first < b.first;
}
int main() {
vector<pair<int, string>> v = {{3, "apple"}, {1, "banana"}, {2, "orange"}};
sort(v.begin(), v.end(), compare);
cout << "Sorted vector of pairs is: ";
for (int i = 0; i < v.size(); i++) {
cout << v[i].first << " " << v[i].second << " ";
}
cout << endl;
return 0;
}
输出结果为:Sorted vector of pairs is: 1 banana 2 orange 3 apple
例子5: 对数组进行反向排序
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
sort(arr, arr + n, greater<int>());
cout << "Reverse sorted array is: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
输出结果为:Reverse sorted array is: 9 6 5 5 5 4 3 3 2 1 1
例子6: 对 vector 进行反向排序
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
sort(v.rbegin(), v.rend(), greater<int>());
cout << "Reverse sorted vector is: ";
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
return 0;
}
输出结果为:Reverse sorted vector is: 9 6 5 5 5 4 3 3 2 1 1
例子7: 对字符串进行反向排序
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool compare(const string& a, const string& b) {
return a > b;
}
int main() {
string str = "hello world";
sort(str.rbegin(), str.rend(), compare);
cout << "Reverse sorted string is: " << str << endl;
return 0;
}
输出结果为:Reverse sorted string is: dlrow olleh
总结
std::sort
函数是C++标准库中常用的排序函数之一,它可以对各种类型的序列进行排序。通过指定不同的比较函数,可以实现不同的排序方式。在实际开发中,我们经常需要对数据进行排序以便进行后续处理或分析。掌握std::sort
函数的使用技巧可以帮助我们更高效地完成这些任务。