c++ 为容器添加比较的方法

在这里我总结一下网上看来的方法,以map为例:

方法一:

#include <iostream>
#include <utility>
#include <string>
#include <map>
using namespace std;
bool my_compare(const string &str1, const string &str2)
{
    return str1 > str2;
}

typedef bool (*comp)(const string &,const string &);
int main(int argc, char *argv[])
{
    map<string, int, comp> my_map(my_compare);
    my_map["wjj"] = 1;
    my_map["lqm"] = 2;
    my_map["mei"] = 3;
    map<string, int,comp>::const_iterator itr = my_map.begin();
    while ( itr != my_map.end()) {
        cout << itr->first << " : " << itr->second << endl;
        itr++;
    }
    return 0;
}

方法二:

#include <iostream>
#include <map>
#include <string>
using namespace std; 
 
class key
{
public:
       float eyesight;
       float height;
 
       key(float x, floaty):eyesight(x), height(y)
       {
       }
 
       friend bool operator < (constkey&,const key&);
};
 
bool operator < (constkey& key1,const key& key2)
{
       // 按eyesight升序 + height升序排列
       if(key1.eyesight != key2.eyesight)      
              return (key1.eyesight < key2.eyesight);
       else
              return (key1.height < key2.height);
      
       // 按eyesight降序 + height降序排列
       //if(key1.eyesight != key2.eyesight)    
       //     return(key1.eyesight > key2.eyesight);
       //else                                 
       //     return(key1.height > key2.height);
 
       // 按eyesight升序 + height降序排列
       //if(key1.eyesight != key2.eyesight)    
       //     return(key1.eyesight < key2.eyesight);
       //else                                 
       //     return(key1.height > key2.height);
 
       // 按eyesight降序 + height升序排列
       //if(key1.eyesight != key2.eyesight)    
       //     return(key1.eyesight > key2.eyesight);
       //else                                 
       //     return(key1.height < key2.height);
}
 
class Student
{
private:
         int id;                   //学号
         string name;              // 姓名
         float eyesight;           //视力
         float height;             //身高
         float chinese;            //语文成绩
         float english;            //英文成绩
         float math;               //数学成绩
public:
       Student(int id, string name,floateyesight,float height,float chinese,float english,float math)
       {
              this->id = id;
              this->name = name;
              this->eyesight = eyesight;
              this->height = height;
              this->chinese = chinese;
              this->english = english;
              this->math = math;
       }
 
       int get_id()
       {
              return id;
       }
 
       string get_name()
       {
              return name;
       }
 
       float get_eyesight()
       {
              return eyesight;
       }
 
       float get_height()
       {
              return height;
       }
 
       float get_chinese()
       {
              return chinese;
       }
 
       float get_english()
       {
              return english;
       }
 
       float get_math()
       {
              return math;
       }
};
 
int main(int argc,char**argv)
{
       map<key,Student> stu_map;
 
       Studentstu4(4, "Dudley",1.1f, 170.2f, 90.5f, 89.5f, 93.0);
       Studentstu3(3, "Chris", 1.1f, 163.4f, 93.5f,90.0f, 83.5f);
       Studentstu2(2, "Bob", 1.5f, 166.6f, 86.0f,98.5f, 85.0f);
       Studentstu1(1, "Andrew", 1.5f, 173.2f, 98.5f,100.0f, 100.f);
 
       stu_map.insert(make_pair(key(stu4.get_eyesight(),stu4.get_height()), stu4));
       stu_map.insert(make_pair(key(stu3.get_eyesight(),stu3.get_height()), stu3));
       stu_map.insert(make_pair(key(stu2.get_eyesight(),stu2.get_height()), stu2));
       stu_map.insert(make_pair(key(stu1.get_eyesight(),stu1.get_height()), stu1));
 
       map<key,Student>::iterator iter;
       for(iter = stu_map.begin(); iter != stu_map.end();++iter)
       {
             cout<< iter->first.eyesight << "\t"<< iter->first.height  << "\t" << iter->second.get_id()<<"\t" <<iter->second.get_name() << endl;
       }
 
       return 0;
}

参考:http://blog.csdn.net/pathuang68/article/details/7526305

        http://blog.csdn.net/challenge_c_plusplus/article/details/7429963

如果还有其他方法还请指教。

posted @ 2012-06-14 13:04  没出没  阅读(535)  评论(0编辑  收藏  举报