C++ STL map key是自定义结构体或类
来源:互联网 发布:奇门排盘软件下载 编辑:程序博客网 时间:2024/06/05 03:00
/// @file 1.cpp/// @brief /// @author EastonWoo /// 0.01/// @date 2015-11-02#include <stdio.h>#include <unistd.h>#include <semaphore.h>#include <pthread.h>#include <string>#include <map>class CKey{public: CKey(std::string str1, std::string str2) { this->str1 = str1; this->str2 = str2; }public: std::string str1; std::string str2;public: bool operator<(const CKey & ct) const // 两个const是必需的。 { if (str1 < ct.str1) { return true; } else if (str1 == ct.str1) { if ( str2 < ct.str2 ) { return true; } } return false; }};int main(int argc, const char *argv[]){ std::map<CKey, std::string> _map; CKey key1("11", "12"); // 被key6覆盖 CKey key6("11", "12"); CKey key5("23", "12"); CKey key7("22", "22"); CKey key2("21", "22"); CKey key3("21", "23"); CKey key4("21", "21"); _map[key1] = "hello"; _map[key2] = "world1"; _map[key3] = "world2"; _map[key4] = "world3"; _map[key5] = "world4"; _map[key6] = "world5"; _map[key7] = "world6"; for(auto it = _map.begin(); it != _map.end(); ++it) { printf("[%s:%s]it->second = %s\n", it->first.str1.c_str(), it->first.str2.c_str(), it->second.c_str()); } return 0;}// 编译,打印// tmp-> g++ 1.cpp -std=c++0x && ./a.out// [11:12]it->second = world5// [21:21]it->second = world3// [21:22]it->second = world1// [21:23]it->second = world2// [22:22]it->second = world6// [23:12]it->second = world4// tmp-> // //// 结果: key5 > key7 > key3 > key2 > key4 > key6 = key1// key1 被 key6 覆盖
0 0
- C++ STL map key是自定义结构体或类
- 【C++】自定义结构体作为map的key
- c++ map中key为类对象或结构体
- Map的key为自定义结构体
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- STL map中key为结构体的用法
- map 自定义结构 存储 STL
- C++类或结构作为map的key值
- STL map使用自定义对象做key
- 自定义结构体做为map里面的key的写法
- key为自定义结构的map
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)
- jQ-ajax
- 虚拟机安装 CentOS 6.7
- SceneKit
- oracle函数测试
- 关于OpenNI2和OpenCV2的那些事——获取彩色图和深度图(XtionProLive)
- C++ STL map key是自定义结构体或类
- SDWebImage
- MVC MVVM
- 程序员面试_Java基础技术
- Git中的AutoCRLF与SafeCRLF换行符问题
- CTP开发——下单
- java 线程池的原理分析
- Objective-C 入门博客链接
- qt背景设置