关于C++中vector和set使⽤sort⽅法进⾏排序C++中vector和set都是⾮常⽅便的容器,
sort⽅法是algorithm头⽂件⾥的⼀个标准函数,能进⾏⾼效的排序,默认是按元素从⼩到⼤排序
将sort⽅法⽤到vector和set中能实现多种符合⾃⼰需求的排序
⾸先sort⽅法可以对静态的数组进⾏排序
1 #include<iostream>
2using namespace std;
3int main(){
4int a[10] = { 9, 0, 1, 2, 3, 7, 4, 5, 100, 10 };
5 sort(a, a +10);
6for (int i = 0; i < 10; i++)
7 cout << a[i] << endl;
8return0;
9 }
运⾏结果:
这⾥可以看到是sort(a,a+10),但是数组a⼀共只有9个元素,为什么是a+10⽽不是a+9呢?
因为sort⽅法实际上最后⼀位地址对应的数是不取的,
⽽且vector,set,map这些容器的end()取出来的值实际上并不是最后⼀个值,⽽end的前⼀个才是最后⼀个值!
需要⽤d()),才能取出容器中最后⼀个元素。
对vector使⽤sort函数:
第⼀种情形:基本类型,如vector<int>,vector<double>,vector<string>也是可以的
1 #include<iostream>
2 #include<vector>
3 #include<algorithm>
4using namespace std;
5int main(){
6 vector<int> a;
7int n = 5;
8while (n--){
9int score;
10 cin >> score;
11 a.push_back(score);
12 }
13//cout <<" a.end()"<< *a.end() << endl; 执⾏这句话会报错!
14 cout << " d)" << *d()) << endl;
15 sort(a.begin(), a.end());
16for (vector<int>::iterator it = a.begin(); it != a.end(); it++){
17 cout << *it << endl;
18 }
19return0;
20 }
执⾏结果:
看到了吗,实际上end的前⼀个指针指向的元素才是插⼊时的最后⼀个值!
排序后从⼩⼤⼤。
第⼆种情形:⽤⾃定义的结构体进⾏sort算法,
这时候需要⾃⼰定义个⽐较函数,因为sort算法是基于容器中的元素是可以两两⽐较的,然后从⼩到⼤排序,所以要⾃定义怎么样才是⼩于('<')
1 #include<iostream>
2 #include<vector>
3 #include<set>
4 #include<string>
5 #include<algorithm>
6using namespace std;
7struct student{
8char name[10];
9int score;
10 };
11//⾃定义“⼩于”
12bool comp(const student &a, const student &b){
13return a.score < b.score;
14 }
15int main(){
16 vector<student> vectorStudents;
17int n = 5;
18while (n--){
19 student oneStudent;
20string name;
21int score;
22 cin >> name >> score;
23 strcpy(oneStudent.name, name.c_str());
24 oneStudent.score = score;
25 vectorStudents.push_back(oneStudent);
26 }
27 cout << "===========排序前================" << endl;
28for (vector<student>::iterator it = vectorStudents.begin(); it != d(); it++){
29 cout << "name: " << it->name << " score: " << it->score << endl;
30 }
31 sort(vectorStudents.begin(),d(),comp);
32 cout << "===========排序后================" << endl;
33for (vector<student>::iterator it = vectorStudents.begin(); it != d(); it++){
34 cout << "name: " << it->name << " score: " << it->score << endl;
35 }
36return0;
37 }
运⾏结果:
不过有时候⼀个排序条件不够,⽐如要求学⽣按分数从⾼到低排序,如果分数相同,则按照年龄从⼤到⼩排序
就需要在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函数
1 #include<iostream>
2 #include<vector>
3 #include<set>
4 #include<string>
5 #include<algorithm>
6using namespace std;
7struct student{
8char name[10];
9int score;
10int age;
11 };
12//⾃定义“⼩于”
13bool comp(const student &a, const student &b){
14if (a.score > b.score)
15return true;
16else if (a.score == b.score && a.age > b.age)
17return true;
18else///这⾥的else return false⾮常重要
19return false;
20 }
21int main(){
22 vector<student> vectorStudents;
23/*set<student> setStudents;*/
24//int n = 5;
25int n = 6;
26while (n--){
27 student oneStudent;
28string name;
29int score;
30int age;
31 cin >> name >> score>>age;
32 strcpy(oneStudent.name, name.c_str());
33 oneStudent.score = score;
34 oneStudent.age = age;
35 vectorStudents.push_back(oneStudent);
36 }
37 cout << "===========排序前================" << endl;
38for (vector<student>::iterator it = vectorStudents.begin(); it != d(); it++){
39 cout << "name: " << it->name << " score: " << it->score << " age: "<<it->age<<endl;
40 }
41 sort(vectorStudents.begin(), d(), comp);
42//sort(setStudents.begin(), d());
include怎么用43 cout << "===========排序后================" << endl;
44for (vector<student>::iterator it = vectorStudents.begin(); it != d(); it++){
45 cout << "name: " << it->name << " score: " << it->score << " age: " << it->age << endl;
46 }
47return0;
48 }
运⾏结果如下:
接下来,对于set做类似的操作。
set是⼀个集合,内部的元素不会重复,同时它会⾃动进⾏排序,也是从⼩到⼤
⽽且set的insert⽅法没有insert(a,cmp)这种重载,所以如果要把结构体插⼊set中,我们就要重载'<'运算符。set⽅法在插⼊的时候也是从⼩到⼤的,那么我们重载⼀下<;运算符让它从⼤到⼩排序
1 #include<iostream>
2 #include<vector>
3 #include<set>
4 #include<string>
5 #include<algorithm>
6using namespace std;
7struct student{
8char name[10];
9int score;
10 };
11//⾃定义“⼩于”
12bool comp(const student &a, const student &b){
13return a.score < b.score;
14 }
15bool operator < (const student & stu1,const student &stu2){
16return stu1.score > stu2.score;
17 }
18int main(){
19//vector<student> vectorStudents;
20set<student> setStudents;
21//int n = 5;
22int n = 6;
23while (n--){
24 student oneStudent;
25string name;
26int score;
27 cin >> name >> score;
28 strcpy(oneStudent.name, name.c_str());
29 oneStudent.score = score;
30 setStudents.insert(oneStudent);
31 }
32 cout << "===========排序前================" << endl;
33for (set<student>::iterator it = setStudents.begin(); it != d(); it++){
34 cout << "name: " << it->name << " score: " << it->score << endl;
35 }
36//sort(setStudents.begin(), d(), comp);
37//cout << "===========排序后================" << endl;
38//for (set<student>::iterator it = setStudents.begin(); it != d(); it++){
39// cout << "name: " << it->name << " score: " << it->score << endl;
40//}
41return0;
42 }
运⾏结果:
我们可以看到,set内元素不会重复,⽽且它按照它所认为的“从⼩到⼤”进⾏了排序
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论