参考博客:https://blog.csdn.net/wangshubo1989/article/details/52806027
最近用到c++ STL中的sort函数,由于不熟悉,查找的博客中也有些小漏洞,走了些弯路。所以自己摘录修改了一篇博客以便记录。
1 数组排序
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test_arr[5];
test_arr[0] = 2;
test_arr[1] = 3;
test_arr[2] = 5;
atest_arrr[3] = 1;
test_arr[4] = 4;
//默认升序排列
sort(test_arr,test_arr+5);
cout << "升序排列: " << endl;
cout << test_arr[0] << ",";
cout << test_arr[1] << ",";
cout << test_arr[2] << ",";
cout << test_arr[3] << ",";
cout << test_arr[4] << endl;
//降序排列
sort(test_arr,test_arr+5,greater<int>());
cout << "降序排列: " << endl;
cout << test_arr[0] << ",";
cout << test_arr[1] << ",";
cout << test_arr[2] << ",";
cout << test_arr[3] << ",";
cout << test_arr[4] << endl;
return 0;
}
2 容器排序
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::vector<int> test_vec;
test_vec.push_back(2);
test_vec.push_back(3);
test_vec.push_back(5);
test_vec.push_back(1);
test_vec.push_back(4);
std::sort(test_vec.begin(),test_vec.end());
for(size_t i=0; i<test_vec.size(); ++i)
std::cout<<test_vec[i]<<std::endl;
return 0;
}
3 重载比较函数-全局
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
class Person
{
public:
// default constructor
Person() : age(0) {}
Person(int age, std::string name) {
this->age = age; this->name = name;
}
int age;
std::string name;
};
inline bool operator<(const Person& a, const Person& b) const //注意此处加const,原博没有导致使用时出错
{
return a.age < b.age;
}
int main()
{
std::vector<Person> persons;
persons.push_back(Person(24,"Calvin"));
persons.push_back(Person(30,"Benny"));
persons.push_back(Person(28,"Alison"));
std::sort(persons.begin(),persons.end());
for(size_t i=0; i<persons.size(); ++i)
std::cout<<persons[i].age<<", "<<persons[i].name<<std::endl;
return 0;
}
4 重载比较函数-成员函数
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
class Person
{
public:
// default constructor
Person() : age(0) {}
Person(int age, std::string name) {
this->age = age; this->name = name;
}
bool operator<(const Person& rhs) const //注意加const
{
return this->age < rhs.age;
}
int age;
std::string name;
};
int main()
{
std::vector<Person> persons;
persons.push_back(Person(24,"Calvin"));
persons.push_back(Person(30,"Benny"));
persons.push_back(Person(28,"Alison"));
std::sort(persons.begin(),persons.end());
for(size_t i=0; i<persons.size(); ++i)
std::cout<<persons[i].age<<", "<<persons[i].name<<std::endl;
return 0;
}
5 函数指针
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
class Person
{
public:
// default constructor
Person() : age(0) {}
Person(int age, std::string name) {
this->age = age; this->name = name;
}
int age;
std::string name;
};
bool Greater(const Person& a, const Person& b)
{
if(a.age == b.age)
return a.name < b.name;
return a.age > b.age;
}
int main()
{
std::vector<Person> persons;
persons.push_back(Person(24,"Calvin"));
persons.push_back(Person(30,"Benny"));
persons.push_back(Person(30,"Alice"));
persons.push_back(Person(28,"Alison"));
std::sort(persons.begin(),persons.end(),Greater);
for(size_t i=0; i<persons.size(); ++i)
std::cout<<persons[i].age<<", "<<persons[i].name<<std::endl;
return 0;
}
6 sort中使用lambda表达式:
std::sort(persons.begin(), persons.end(), [](const Person &a, const Person &b) { return ((*a).age < (*b).age); });