转载自:https://www.cnblogs.com/zhouxiaosong/p/5557990.html
C++中vector和set都是非常方便的容器,
sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序
将sort方法用到vector和set中能实现多种符合自己需求的排序
首先sort方法可以对静态的数组进行排序
#include
using namespace std;
int main(){
int a[10] = { 9, 0, 1, 2, 3, 7, 4, 5, 100, 10 };
sort(a, a +10);
for (int i = 0; i < 10; i++)
cout << a[i] << endl;
return 0;
}
运行结果:
这里可以看到是sort(a,a+10),但是数组a一共只有9个元素,为什么是a+10而不是a+9呢?
因为sort方法实际上最后一位地址对应的数是不取的,
而且vector,set,map这些容器的end()取出来的值实际上并不是最后一个值,而end的前一个才是最后一个值!
需要用prev(xxx.end()),才能取出容器中最后一个元素。
对vector使用sort函数:
第一种情形:基本类型,如vector,vector,vector也是可以的
#include
#include
#include
using namespace std;
int main(){
vector<int> a;
int n = 5;
while (n--){
int score;
cin >> score;
a.push_back(score);
}
//cout <<" a.end()"<< *a.end() << endl; 执行这句话会报错!
cout << " prev(a.end)" << *prev(a.end()) << endl;
sort(a.begin(), a.end());
for (vector<int>::iterator it = a.begin(); it != a.end(); it++){
cout << *it << endl;
}
return 0;
}
运行结果:
看到了吗,实际上end的前一个指针指向的元素才是插入时的最后一个值!
排序后从小大大。
第二种情形:用自定义的结构体进行sort算法,
这时候需要自己定义个比较函数,因为sort算法是基于容器中的元素是可以两两比较的,然后从小到大排序,所以要自定义怎么样才是小于(’<’)
#include
#include
#include
#include
#include
using namespace std;
struct student{
char name[10];
int score;
};
//自定义“小于”
bool comp(const student &a, const student &b){
return a.score < b.score;
}
int main(){
vector<student> vectorStudents;
int n = 5;
while (n--){
student oneStudent;
string name;
int score;
cin >> name >> score;
strcpy(oneStudent.name, name.c_str());
oneStudent.score = score;
vectorStudents.push_back(oneStudent);
}
cout << "===========排序前================" << endl;
for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
cout << "name: " << it->name << " score: " << it->score << endl;
}
sort(vectorStudents.begin(),vectorStudents.end(),comp);
cout << "===========排序后================" << endl;
for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
cout << "name: " << it->name << " score: " << it->score << endl;
}
return 0;
}
运行结果:
不过有时候一个排序条件不够,比如要求学生按分数从高到低排序,如果分数相同,则按照年龄从大到小排序
就需要在comp自定义函数里面修改一下判断了,原来是直接return a.score < b.score
现在就需要判断
if (a.score > b.score)
return true;
else if (a.score == b.score && a.age > b.age)
return true;
else
return false;
这里一定要记得else return false!!!有一次比赛的时候写到这个函数,有三个判断条件,结果忘了这茬,总是报错,到后来有点着急了就自己手动实现了一下写了三个比较函数,调用了三次sort函数!!!!!
#include
#include
#include
#include
#include
using namespace std;
struct student{
char name[10];
int score;
int age;
};
//自定义“小于”
bool comp(const student &a, const student &b){
if (a.score > b.score)
return true;
else if (a.score == b.score && a.age > b.age)
return true;
else ///这里的else return false非常重要!!!!!
return false;
}
int main(){
vector<student> vectorStudents;
/*set setStudents;*/
//int n = 5;
int n = 6;
while (n--){
student oneStudent;
string name;
int score;
int age;
cin >> name >> score>>age;
strcpy(oneStudent.name, name.c_str());
oneStudent.score = score;
oneStudent.age = age;
vectorStudents.push_back(oneStudent);
}
cout << "===========排序前================" << endl;
for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
cout << "name: " << it->name << " score: " << it->score << " age: "<<it->age<<endl;
}
sort(vectorStudents.begin(), vectorStudents.end(), comp);
//sort(setStudents.begin(), setStudents.end());
cout << "===========排序后================" << endl;
for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
cout << "name: " << it->name << " score: " << it->score << " age: " << it->age << endl;
}
return 0;
}
运行结果:
接下来,对于set做类似的操作。
set是一个集合,内部的元素不会重复,同时它会自动进行排序,也是从小到大
而且set的insert方法没有insert(a,cmp)这种重载,所以如果要把结构体插入set中,我们就要重载’<'运算符。
set方法在插入的时候也是从小到大的,那么我们重载一下<运算符让它从大到小排序
#include
#include
#include
#include
#include
using namespace std;
struct student{
char name[10];
int score;
};
//自定义“小于”
bool comp(const student &a, const student &b){
return a.score < b.score;
}
bool operator < (const student & stu1,const student &stu2){
return stu1.score > stu2.score;
}
int main(){
//vector vectorStudents;
set<student> setStudents;
//int n = 5;
int n = 6;
while (n--){
student oneStudent;
string name;
int score;
cin >> name >> score;
strcpy(oneStudent.name, name.c_str());
oneStudent.score = score;
setStudents.insert(oneStudent);
}
cout << "===========排序前================" << endl;
for (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++){
cout << "name: " << it->name << " score: " << it->score << endl;
}
//sort(setStudents.begin(), setStudents.end(), comp);
//cout << "===========排序后================" << endl;
//for (set::iterator it = setStudents.begin(); it != setStudents.end(); it++){
// cout << "name: " << it->name << " score: " << it->score << endl;
//}
return 0;
}