STL sort演算法中的比較函式
阿新 • • 發佈:2019-01-11
排序,既陌生又熟悉的名詞。
排序,成為面試官中喜歡問的演算法問題。
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); });