博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2、以自定义struct或struct指针作为map的Key
阅读量:4628 次
发布时间:2019-06-09

本文共 3575 字,大约阅读时间需要 11 分钟。

若干问题

 

struct Node {    int k, b;    friend bool operator <(Node a, Node b) {        return a.k < b.k;    }}node1, node2;map
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
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

 

 

 

1、以结构体为Key

map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),红黑树具有对数据自动排序(默认是以less<>升序对元素排序(排序准则也可以修改))的功能,因此在map内部所有的关键字都是有序的。当key为基本数据类型时,不存在问题。但是当关键字是一个结构体时,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候就会出错,下面给出解决这个问题的方法:

对操作符"<"进行重载(不可以重载大于号)

#include #include 
#include
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
mapStudent; map
::iterator it; StudentInfo.id = 1; StudentInfo.name = "student_one"; mapStudent.insert(pair
(StudentInfo, 90)); StudentInfo.id = 1; StudentInfo.name = "student_two"; mapStudent.insert(pair
(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]);  都可以

 

重载的部分可以写到结构体外,但有三点要求:

1、把friend去掉,把小于号改成一对小括号。

2、用struct把函数包装起来。

3、map的定义方式改为map<Node, int, cmp> mapStudent;

如:

#include #include 
#include
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
mapStudent;map
::iterator it;int main(){ int nSize; //用学生信息映射分数 StudentInfo.id = 1; StudentInfo.name = "student_one"; mapStudent.insert(pair
(StudentInfo, 90)); StudentInfo.id = 1; StudentInfo.name = "student_two"; mapStudent.insert(pair
(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]); 也可以

 

 

 

2、以结构体指针为Key,可以不重载<号,因为地址可以比较大小。

但是也可以根据指针指向的内容重载小于号,但此时重载函数必须放在自定义的结构体外面。(原因不详。。。)

#include #include 
#include
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
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
::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); } }}

 

转载于:https://www.cnblogs.com/fuqia/p/9511994.html

你可能感兴趣的文章
ubuntu安装好新字体后 需要更新字体缓冲
查看>>
Hadoop配置文件参数详解
查看>>
c基础补充
查看>>
【读后感3】高效程序员的45个习惯
查看>>
浅谈HTTP和HTTPS
查看>>
2016/09/03
查看>>
Mplayer 音频解码分析
查看>>
实践二
查看>>
SQL SERVER动态列名
查看>>
【译】如何掌握一门新的学科
查看>>
vi中将tab键转化为空格
查看>>
Python的getattr()-2017年6月7日
查看>>
JavaMail 发送邮件
查看>>
linux分区
查看>>
ACM对拍程序
查看>>
BigDemical、BigInteger
查看>>
P1980【洛谷】 计数问题 解题报告
查看>>
protected的使用注意
查看>>
笑笑得了手足口
查看>>
Java获取2个日期里面的所有月份
查看>>