STL sort算法中的比较函数
2016-10-13 13:12
369 查看
排序,既陌生又熟悉的名词。
排序,成为面试官中喜欢问的算法问题。
c++ STL中为我们提供了std::sort, 所以今天我们不是来描述各种排序算法的实现,而是看看怎么使用stl为我们提供的sort。
先预热,代码:
从输入结果可以看出,sort默认是按照升序进行排序。
按照降序进行排序:
其中用到了std::greater()。
sort中使用迭代器:
定义自己的比较函数–全局函数:
定义自己的比较函数–成员函数:
函数指针作为sort的比较函数:
sort中使用lambda表达式:
排序,成为面试官中喜欢问的算法问题。
c++ STL中为我们提供了std::sort, 所以今天我们不是来描述各种排序算法的实现,而是看看怎么使用stl为我们提供的sort。
先预热,代码:
#include <algorithm> #include <iostream> #include <string> 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; std::sort(test_arr,test_arr+5); std::cout<<test_arr[0]<<"\n"; std::cout<<test_arr[1]<<"\n"; std::cout<<test_arr[2]<<"\n"; std::cout<<test_arr[3]<<"\n"; std::cout<<test_arr[4]<<std::endl; return 0; }
从输入结果可以看出,sort默认是按照升序进行排序。
按照降序进行排序:
#include <algorithm> #include <iostream> #include <string> #include <functional> int main() { int test_arr[5]; test_arr[0] = 2; test_arr[1] = 3; test_arr[2] = 5; test_arr[3] = 1; test_arr[4] = 4; std::sort(test_arr,test_arr+5,std::greater<int>()); std::cout<<test_arr[0]<<"\n"; std::cout<<test_arr[1]<<"\n"; std::cout<<test_arr[2]<<"\n"; std::cout<<test_arr[3]<<"\n"; std::cout<<test_arr[4]<<std::endl; return 0; }
其中用到了std::greater()。
sort中使用迭代器:
#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; }
定义自己的比较函数–全局函数:
#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) { 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; }
定义自己的比较函数–成员函数:
#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) { 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; }
函数指针作为sort的比较函数:
#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; }
sort中使用lambda表达式:
std::sort(persons.begin(), persons.end(), [](const Person &a, const Person &b) { return ((*a).age < (*b).age); });
相关文章推荐
- STL sort算法中的比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- 火眼睛睛查coredump(stl sort)------永远让比较函数对相同元素返回false
- STL中比较常用的容器是vector,set和map,比较常用的算法有Sort等。
- STL中sort、priority_queue、map、set的自定义比较函数
- C++ STL list 成员函数 sort算法分析
- STL中sort、priority_queue、map、set的自定义比较函数
- 算法学习 - STL的排序函数(sort)使用
- 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法
- c++ 提高4 map容器 共性机制 使用时机 比较| STL算法 算法基础仿函数 谓词 函数适配器 遍历算法
- 算法导论—快排及优化以及和STL sort 的比较
- 火眼睛睛查coredump(stl sort)------永远让比较函数对相同元素返回false
- 利用STL中的Sort比较自定义结构体
- STL中map的比较函数
- STL priority_queue sort 自定义比较终极模板
- 【C++研发面试笔记】21. 常用算法-STL中常用算法函数
- 使用C++标准库sort自定义比较函数导致死循环问题
- 利用STL中的sort算法对向量等容器进行排序
- LeetCode/sort(地址A,地址B)和qsort(首地址,元素个数,sizeof(),cmp比较函数)的使用
- C++:STL中sort()函数的用法总结