struct作为map的key
2016-08-12 15:23
274 查看
某些情况下,只用一个int或者一个string,无法满足需求,需要多个字段联合作为map的key,如何实现? 请看如下代码:
#include <stdio.h>
#include <stdlib.h>
#include <map>
struct mapkey
{
int sceneID;
int teamID;
///>重载== 运算符
bool operator == ( const mapkey& obj) const
{
if( sceneID == obj.sceneID && teamID == obj.teamID )
{
return true;
}
return false;
}
///>重载< 运算符
bool operator < ( const mapkey& obj ) const
{
if( sceneID < obj.sceneID )
{
return true;
}
if( sceneID == obj.sceneID && teamID < obj.teamID )
{
return true;
}
return false;
}
};
int main()
{
std::map<mapkey, int> mapInfo;
mapInfo.clear();
mapkey stc;
stc.sceneID = 1;
stc.teamID = 5;
mapInfo.insert( std::make_pair(stc, 1) );
stc.sceneID = 2;
stc.teamID = 4;
mapInfo.insert( std::make_pair(stc, 5) );
stc.sceneID = 2;
stc.teamID = 3;
mapInfo.insert( std::make_pair(stc, 6) );
for( std::map<mapkey, int>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++ )
{
printf("scenid:%llu, teamID:%llu, value:%d\n", it->first.sceneID, it->first.teamID, it->second);
}
stc.sceneID = 2;
stc.teamID = 3;
std::map<mapkey, int>::iterator itFind = mapInfo.find( stc );
if( itFind == mapInfo.end() )
{
printf("cannot find key, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
else
{
printf("find key success, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
stc.sceneID = 2;
stc.teamID = 8;
itFind = mapInfo.find( stc );
if( itFind == mapInfo.end() )
{
printf("cannot find key, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
else
{
printf("find key success, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
}
编译:
$ g++ -g -o hello stckey.cpp
运行:
$ ./hello
输出如下:
$ ./hello
scenid:1, teamID:5, value:1
scenid:2, teamID:3, value:6
scenid:2, teamID:4, value:5
find key success, sceneID:2, teamID:3
cannot find key, sceneID:2, teamID:8
#include <stdio.h>
#include <stdlib.h>
#include <map>
struct mapkey
{
int sceneID;
int teamID;
///>重载== 运算符
bool operator == ( const mapkey& obj) const
{
if( sceneID == obj.sceneID && teamID == obj.teamID )
{
return true;
}
return false;
}
///>重载< 运算符
bool operator < ( const mapkey& obj ) const
{
if( sceneID < obj.sceneID )
{
return true;
}
if( sceneID == obj.sceneID && teamID < obj.teamID )
{
return true;
}
return false;
}
};
int main()
{
std::map<mapkey, int> mapInfo;
mapInfo.clear();
mapkey stc;
stc.sceneID = 1;
stc.teamID = 5;
mapInfo.insert( std::make_pair(stc, 1) );
stc.sceneID = 2;
stc.teamID = 4;
mapInfo.insert( std::make_pair(stc, 5) );
stc.sceneID = 2;
stc.teamID = 3;
mapInfo.insert( std::make_pair(stc, 6) );
for( std::map<mapkey, int>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++ )
{
printf("scenid:%llu, teamID:%llu, value:%d\n", it->first.sceneID, it->first.teamID, it->second);
}
stc.sceneID = 2;
stc.teamID = 3;
std::map<mapkey, int>::iterator itFind = mapInfo.find( stc );
if( itFind == mapInfo.end() )
{
printf("cannot find key, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
else
{
printf("find key success, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
stc.sceneID = 2;
stc.teamID = 8;
itFind = mapInfo.find( stc );
if( itFind == mapInfo.end() )
{
printf("cannot find key, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
else
{
printf("find key success, sceneID:%llu, teamID:%llu\n", stc.sceneID, stc.teamID);
}
}
编译:
$ g++ -g -o hello stckey.cpp
运行:
$ ./hello
输出如下:
$ ./hello
scenid:1, teamID:5, value:1
scenid:2, teamID:3, value:6
scenid:2, teamID:4, value:5
find key success, sceneID:2, teamID:3
cannot find key, sceneID:2, teamID:8
相关文章推荐
- 关于SpringOpenSessionInViewFilter的配置及处理方案
- GO-log日志封装
- List排序
- MFC AfxMessageBox默认标题修改
- 浏览器版本过低
- gfw列表
- Threadpool dump
- smarty半小时快速上手入门教程
- 格式怎样把ppt转换成word文档
- WiFi Direct的Android端调用说明
- 【Deep Learning】Github上关注最多的53个深度学习项目
- iOS设置某个界面强制横屏,进入就横屏
- RecyclerView使用完全指南,是时候体验新控件了(一)
- <base href="<%=basePath%>">
- iOS中几种数据持久化方案
- python dict 数据结构遍历
- 软考之我见——上午题、案例题
- opencv的haarcascade模型人脸检测输出框的得分总结
- java Thread 线程的.Join()方法
- 【51node】-1119- 机器人走方格(费马小定理,逆元,快速幂)