原博客:https://www.cnblogs.com/aaronzlq/p/3612629.html
C++11引入了很多新特性,比如auto ,比如 for(type v : container)等。
数据结构方面最抢眼的应该是引入了unordered_set和unordered_map。比起普通的set 和 map,其内部不再是红黑树排关键字了,而是用的哈系表;来提高查找效率。
不过对于结构体的存储和映射,却没怎么发现别人讲,刚看了篇文章学会了=_=:http://choorucode.com/2012/06/26/c-using-unordered_set/
Mark一下,贴下自己的代码,也方便别人查使用方法。
g++ 编译时注意添加: -std=c++11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <iostream> #include <cstdio> #include <set> #include <unordered_set> #include <unordered_map> using namespace std; struct Node { Node( int _x, int _y):x(_x), y(_y) {} int x, y; bool operator == ( const Node &t) const { return x==t.x ; } }; struct NodeHash { std:: size_t operator () ( const Node &t) const { return t.x * 100 + t.y; } }; unordered_set <Node, NodeHash> h_set; unordered_map <Node, string, NodeHash> h_map; int main(){ unordered_map<Node,string,NodeHash>::iterator arr; h_set.insert(Node(1, 2)); int x, y; cin >> x >> y; if (h_set.find(Node(x, y)) == h_set.end()) cout << "Not found" << endl; else cout << "Found succeed" << endl; h_map[Node(1, 2)] = "World" ; cout << h_map[Node(1, 2)] << endl; return 0; } /* 输入: 1 2 输出: 1 2 Found succeed World */ |
分类:
STL
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具