stl中的排序问题

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


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

#include 
#include 

using namespace std;

int main(int argc, char **argv)
{
vector 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 
#include 

using namespace std;

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

int main(int argc, char **argv)
{
vector 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函数进行判断输的大小,系统默认ab时返回为真,那么最终得到的排序结果也相应的从小到大变成从大到小。

map中的排序:

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


  1. #include   
  2. #include   
  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.        mapint>mapStudent;  
  14.        mapint>::iterator iter;  
  15.        StudentInfo studentInfo;  
  16.        studentInfo.nID = 1;  
  17.        studentInfo.strName = "student_one"  
  18.        mapStudent.insert(pairint>(studentInfo, 90));  
  19.        studentInfo.nID = 2;  
  20.        studentInfo.strName = "student_two";  
  21.        mapStudent.insert(pairint>(studentInfo, 80));  
  22.        for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)  
  23.          cout<first.nID<first.strName<second<
  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   
  2. #include   
  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.        mapint, sort>mapStudent;  
  23.        StudentInfo studentInfo;  
  24.        studentInfo.nID = 1;  
  25.        studentInfo.strName = "student_one";  
  26.        mapStudent.insert(pairint>(studentInfo, 90));  
  27.        studentInfo.nID = 2;  
  28.        studentInfo.strName = "student_two";  
  29.        mapStudent.insert(pairint>(studentInfo, 80));  


你可能感兴趣的:(c++)