二、以自定義struct或struct指針做爲map的Key

若干問題node

 

struct Node { int k, b; friend bool operator <(Node a, Node b) { return a.k < b.k; } }node1, node2; map<Node, int> mp; int main() { node1.k = 1; node1.b = 1; mp[node1] = 1; node1.k = 1; node1.b = 2; printf("%d\n", mp.count(node1)); //輸出1
    return 0; }
View Code

 

struct Node { int k, b; friend bool operator <(Node a, Node b) { if (a.k != b.k) return a.k < b.k; else return a.b < b.b; } }node1, node2; map<Node, int> mp; int main() { node1.k = 1; node1.b = 1; mp[node1] = 1; node1.k = 1; node1.b = 2; printf("%d\n", mp.count(node1)); //輸出0
    return 0; }
View Code

 

 

 

一、以結構體爲Keyios

map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),紅黑樹具備對數據自動排序(默認是以less<>升序對元素排序(排序準則也能夠修改))的功能,所以在map內部全部的關鍵字都是有序的。當key爲基本數據類型時,不存在問題。可是當關鍵字是一個結構體時,涉及到排序就會出現問題,由於它沒有小於號操做,insert等函數在編譯的時候就會出錯,下面給出解決這個問題的方法:less

對操做符"<"進行重載(不能夠重載大於號)ide

#include <map> #include <iostream> #include <string>
using namespace std; struct Node{ int id; string name; friend bool operator < (Node a,Node b) { //指定排序策略,按id排序,若是id相等的話,按name排序
        if (a.id != b.id) return a.id > b.id; else return a.name > b.name; } }StudentInfo, *pStudentInfo;  //學生信息

int main(){ int nSize; //用學生信息映射分數
    map<Node, int> mapStudent; map<Node, int>::iterator it; StudentInfo.id = 1; StudentInfo.name = "student_one"; mapStudent.insert(pair<Node, int>(StudentInfo, 90)); StudentInfo.id = 1; StudentInfo.name = "student_two"; mapStudent.insert(pair<Node, int>(StudentInfo, 80)); for (it = mapStudent.begin(); it != mapStudent.end(); it++) cout << it->first.id << " " << it->first.name << " " << it->second << endl; }

 

printf("%d",mp.find(StudentInfo)->second); printf("%d",mp[StudentInfo]);  均可以

 

重載的部分能夠寫到結構體外,但有三點要求:函數

一、把friend去掉,把小於號改爲一對小括號。spa

二、用struct把函數包裝起來。指針

三、map的定義方式改成map<Node, int, cmp> mapStudent;code

如:blog

#include <map> #include <iostream> #include <string>
using namespace std; struct Node{ int id; string name; }StudentInfo, *pStudentInfo;  //學生信息

struct cmp { bool operator () (Node a, Node b) { //指定排序策略,按nID排序,若是nID相等的話,按strName排序
        if (a.id != b.id) return a.id > b.id; else return a.name > b.name; } }; map<Node, int, cmp> mapStudent; map<Node, int, cmp>::iterator it; int main(){ int nSize; //用學生信息映射分數
 StudentInfo.id = 1; StudentInfo.name = "student_one"; mapStudent.insert(pair<Node, int>(StudentInfo, 90)); StudentInfo.id = 1; StudentInfo.name = "student_two"; mapStudent.insert(pair<Node, int>(StudentInfo, 80)); for (it = mapStudent.begin(); it != mapStudent.end(); it++) cout << it->first.id << " " << it->first.name << " " << it->second << endl; }

 

printf("%d",mp.find(StudentInfo)->second); printf("%d",mp[StudentInfo]); 也能夠

 

 

 

二、以結構體指針爲Key,能夠不重載<號,由於地址能夠比較大小。排序

可是也能夠根據指針指向的內容重載小於號,但此時重載函數必須放在自定義的結構體外面。(緣由不詳。。。)

#include <map> #include <iostream> #include <cstdio>
using namespace std; struct Key { int x,y; }*ptr; struct CompareKey { bool operator()(Key *in_a, Key *in_b) { return in_a->x < in_b->x; } }; map<Key *, int, CompareKey> mp; int main() { for (int i = 0; i < 10; i++) { Key *k = new Key; if(i==6) ptr=k; k->x = i; k->y = i+1; mp.insert(make_pair(k, i)); } map<Key *, int, CompareKey>::iterator it; for(it=mp.begin();it!=mp.end();it++){ if(it->first==ptr){ printf("%d %d %d\n",it->first->x,it->first->y,it->second); } } }
相關文章
相關標籤/搜索