stl中map自定义比较函数

来源:互联网 发布:听新闻学英语软件 编辑:程序博客网 时间:2024/05/18 20:13

stl中map自定义比较函数

标签: stl中map自定义比较函数
 757人阅读 评论(0) 收藏 举报
 分类:

目录(?)[+]

在stl中自定义map比较函数有两种方式,方式一在类中重载<操作符,方式二仿比较函数

1、方式一:重载<操作符

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <map>  
  3. #include <string>  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. class Key  
  8. {  
  9. public:  
  10.     Key(string name, int age)  
  11.     {  
  12.         this->name=name;  
  13.         this->age=age;  
  14.     }  
  15.     ~Key(){}  
  16.     string getName()  
  17.     {  
  18.         return name;  
  19.     }  
  20.     int getAge()  
  21.     {  
  22.         return age;  
  23.     }  
  24.     bool operator<(const Key& key) const  
  25.     {  
  26.         if(age!=key.age)  
  27.         {  
  28.             return age<key.age;   
  29.         }  
  30.         else  
  31.         {  
  32.             return false;  
  33.         }  
  34.     }  
  35. private:  
  36.     string name;  
  37.     int age;  
  38. };  
  39.   
  40. int main(void)  
  41. {  
  42.     Key key1("cjc",25);  
  43.     Key key2("ldb",26);  
  44.     Key key3("jzm",27);  
  45.   
  46.     map<Key,string> s;  
  47.     s[key1]="He is a boy.";  
  48.     s[key2]="He is a student.";  
  49.     s[key3]="He is a old man.";  
  50.   
  51.     map<Key,string>::iterator it=s.begin();  
  52.     while(it!=s.end())  
  53.     {  
  54.         cout<<"Name : "<<((Key)it->first).getName()<<endl;  
  55.         cout<<"Age : "<<((Key)it->first).getAge()<<endl;  
  56.         cout<<"Desc : "<<it->second<<endl;  
  57.         cout<<"--------------------------------------"<<endl;  
  58.         it++;  
  59.     }  
  60.   
  61.     system("pause");  
  62.     return 0;  
  63. }  


2、方式二:仿比较函数

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include <map>  
  3. #include <string>  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. typedef struct key  
  8. {  
  9.     string name;  
  10.     int age;  
  11. }Key;  
  12.   
  13. struct cmpKey  
  14. {  
  15.     bool operator()(const Key& key1, const Key& key2)  
  16.     {  
  17.         if(key1.age!=key2.age)  
  18.         {  
  19.             return key1.age<key2.age;  
  20.         }  
  21.         else  
  22.         {  
  23.             return false;  
  24.         }  
  25.     }  
  26. };  
  27.   
  28. int _tmain(int argc, _TCHAR* argv[])  
  29. {  
  30.     map<Key,string,cmpKey> s;  
  31.   
  32.     Key key1,key2,key3;  
  33.       
  34.     key1.age=24;  
  35.     key1.name="cjc";  
  36.     s[key1]="He is a boy!";  
  37.   
  38.     key2.age=24;  
  39.     key2.name="ldb";  
  40.     s[key2]="He is a student!";  
  41.   
  42.     key3.age=26;  
  43.     key3.name="jzm";  
  44.     s[key3]="He is an old man!";  
  45.   
  46.     map<Key,string,cmpKey>::iterator it=s.begin();  
  47.     while(it!=s.end())  
  48.     {  
  49.         cout<<"Age : "<<it->first.age<<endl;  
  50.         cout<<"Name : "<<it->first.name<<endl;  
  51.         cout<<"Desc : "<<it->second<<endl;  
  52.         cout<<"---------------------------------"<<endl;  
  53.         it++;  
  54.     }  
  55.   
  56.     system("pause");  
  57.     return 0;  
  58. }  
原创粉丝点击