c++ map备忘
来源:互联网 发布:linux初学者书籍 编辑:程序博客网 时间:2024/05/21 04:25
例子
#include<iostream>#include<map>using namespace std;int main(){ map<string,string> mymap; mymap["animal1"] = "dog"; mymap["animal2"] = "cat"; mymap["animal3"] = "pig"; auto it = mymap.find("animal2"); if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; //animal2 cat } cout<<mymap.count("animal2")<<endl; //1 //*********lower_bound(k)************ //returns an iterator to the first element with key not less than k cout<<"*********lower_bound************"<<endl; it = mymap.lower_bound("animal2"); if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; //animal2 cat } it = mymap.lower_bound("animal0"); if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; //animal1 dog } it = mymap.lower_bound("animal5"); //it == mymap.end() if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; } //*********upper_bound(k)************ //returns an iterator to the first element with key greater than k cout<<"*********upper_bound************"<<endl; it = mymap.upper_bound("animal2"); if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; //animal3 pig } it = mymap.upper_bound("animal0"); if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; //animal1 dog } it = mymap.upper_bound("animal5"); //it == mymap.end() if(it != mymap.end()){ cout<<it->first<<" "<<it->second<<endl; } //*********equal_range(k)************ //returns a pair of iterators denoting the elements with key k //the first iterator is equal to what the lower_bound return //the second iterator is equal to what the upper_bound return cout<<"*********equal_range************"<<endl; auto it1 = mymap.equal_range("animal2"); if(it1.first!=mymap.end()){ cout<<it1.first->first<<" "<<it1.first->second<<endl; //animal2 cat cout<<it1.second->first<<" "<<it1.second->second<<endl; //animal3 pig } it1 = mymap.equal_range("animal0"); if(it1.first != mymap.end()){ cout<<it1.first->first<<" "<<it1.first->second<<endl; //animal1 dog cout<<it1.second->first<<" "<<it1.second->second<<endl; //animal1 dog } it1 = mymap.equal_range("animal5"); //it1.first == mymap.end() and it1.second == mymap.end() if(it1.first != mymap.end()){ cout<<it1.first->first<<" "<<it1.first->second<<endl; cout<<it1.second->first<<" "<<it1.second->second<<endl; } return 0;}
结果
animal2 cat
1
****lower_bound*******
animal2 cat
animal1 dog
****upper_bound*******
animal3 pig
animal1 dog
****equal_range*******
animal2 cat
animal3 pig
animal1 dog
animal1 dog
0 0
- 备忘 - 迭代Map
- c++ map备忘
- Android Google Map V2 备忘
- 《Pointers on C》备忘
- C 宏定义备忘
- C++STL的备忘
- [c++]宏备忘
- <<Effective C++>>备忘
- C 学习备忘
- C备忘笔记
- C字符串操作备忘
- C语言学习备忘
- c ++ 总结与备忘
- C语言基础知识备忘
- c++map
- c++MAP
- map(c++)
- c++map
- JavaScript高级程序设计之DOM 扩展之专有扩展之插入文本第11.4.4讲
- 安卓引入library后R文件无法生成
- Ext.data.Store 详解
- 一些常用的第三方库
- AnjularJS 学习笔记(数据的绑定)
- c++ map备忘
- IPC——命名管道
- 【C++函数】strcat()
- Android Fragment完全解析
- 【LeetCode从零单刷】Longest Increasing Subsequence
- notepad++如何运行浏览器
- 详解linux下的串口通讯开发
- bookboon
- 如何在MySQL中设置外键约束以及外键的作用