stl中的排序问题

本文详细介绍了C++标准库中的几种容器(vector、list、deque、map)的排序方法,包括默认升序排序及如何实现降序排序。此外,还探讨了map容器中的排序策略,包括重载小于号运算符和使用仿函数进行定制化排序。

家里只介绍关于list, vector, deque, map的排序,其中又分米两类,一是本身具有自动排序功能的,如:map,另一种是本身带有排序函数sort的,如:list, vector, deque


由sort函数排序的,这里以vector为例:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char **argv)
{
vector<int> vNum;
vNum.push_back(1);
vNum.push_back(3);
vNum.push_back(9);
vNum.push_back(7);
vNum.push_back(11);

cout << "before sort: " << endl;
for (it=vNum.begin(); it!=vNum.end(); it++) {
    cout << *it << "  ";
}
cout << endl;

vNum.sort(vNum.begin(), vNum.end());
cout << "after sort: " << endl;
for(it=vNum.begin(); it!=vNum.end(); it++) {
    cout << *it << "  ";
}
cout << endl;

}

运行结果如下:

before sort:

1  3  9  7  11

after sort:

1  3  7  9  11

排序结果默认为从小到大排序,如果想店排序为从大到小,只需要在sort中加入一个比较函数comp():

#include <iostream>
#include <vector>

using namespace std;

bool comp(const int &a, const int &b)
{
    return a>b;
} 

int main(int argc, char **argv)
{
vector<int> vNum;
vNum.push_back(1);
vNum.push_back(3);
vNum.push_back(9);
vNum.push_back(7);
vNum.push_back(11);

cout << "before sort: " << endl;
for (it=vNum.begin(); it!=vNum.end(); it++) {
    cout << *it << "  ";
}
cout << endl;

vNum.sort(vNum.begin(), vNum.end(), comp);
cout << "after sort: " << endl;
for(it=vNum.begin(); it!=vNum.end(); it++) {
    cout << *it << "  ";
}
cout << endl;

}

运行结果如下:

before sort:

1  3  9  7  11

after sort:

11  9  7  3  1


出现家样的结果是因为,比较时sort函数根据comp函数进行判断输的大小,系统默认a<b时返回真,于是从小到大排,而我的comp函数设定为a>b时返回为真,那么最终得到的排序结果也相应的从小到大变成从大到小。

map中的排序:

第一种:小于号重载,程序举例


  1. #include <map>  
  2. #include <string>  
  3. uing namespace std;  
  4. Typedef struct tagStudentInfo  
  5. {  
  6.        int      nID;  
  7.        String   strName;  
  8. }StudentInfo, *PStudentInfo;  //学生信息  
  9. int main()  
  10. {  
  11.        int nSize;  
  12.        //用学生信息映射分数  
  13.        map<StudentInfo, int>mapStudent;  
  14.        map<StudentInfo, int>::iterator iter;  
  15.        StudentInfo studentInfo;  
  16.        studentInfo.nID = 1;  
  17.        studentInfo.strName = "student_one"  
  18.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  19.        studentInfo.nID = 2;  
  20.        studentInfo.strName = "student_two";  
  21.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  
  22.        for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
  23.          cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;  
  24. }  
以上程序是无法编译通过的,只要重载小于号,就OK了,如下:

  1. Typedef struct tagStudentInfo  
  2. {  
  3.        int      nID;  
  4.        String   strName;  
  5.        Bool operator < (tagStudentInfo const& _A) const  
  6.        {  
  7.               //这个函数指定排序策略,按nID排序,如果nID相等的话,按strName排序  
  8.               If(nID < _A.nID)  return true;  
  9.               If(nID == _A.nID) return strName.compare(_A.strName) < 0;  
  10.               Return false;  
  11.        }  
  12. }StudentInfo, *PStudentInfo;  //学生信息  
第二种:仿函数的应用,这个时候结构体中没有直接的小于号重载,程序说明

  1. #include <map>  
  2. #include <string>  
  3. using namespace std;  
  4. Typedef struct tagStudentInfo  
  5. {  
  6.        int      nID;  
  7.        String   strName;  
  8. }StudentInfo, *PStudentInfo;  //学生信息  
  9. class sort  
  10. {  
  11.        Public:  
  12.        Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const  
  13.        {  
  14.               If(_A.nID < _B.nID) return true;  
  15.               If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;  
  16.               Return false;  
  17.        }  
  18. };  
  19. int main()  
  20. {  
  21.        //用学生信息映射分数  
  22.        map<StudentInfo, int, sort>mapStudent;  
  23.        StudentInfo studentInfo;  
  24.        studentInfo.nID = 1;  
  25.        studentInfo.strName = "student_one";  
  26.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));  
  27.        studentInfo.nID = 2;  
  28.        studentInfo.strName = "student_two";  
  29.        mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值