【c/c++】map
2016-01-22 20:55
453 查看
#include <iostream> #include <map> #include<string> using namespace std; //http://www.cplusplus.com/reference/map/map/ //This class couples together a pair of values, which may be of different types (T1 and T2). //The individual values can be accessed through its public members first and second. typedef pair<string, int> PAIR; //make_pair:Construct pair object //ostream& operator<<(ostream& out, const PAIR & p) { //return out << p.first << "\t" << p.second; //} int main() { /* map<string, int> name_score_map; name_score_map["LiMin"] = 90; name_score_map["ZiLinMi"] = 79; name_score_map["BoB"] = 92; name_score_map.insert(make_pair("Bing", 99)); name_score_map.insert(make_pair("Albert", 86)); for (map<string, int>::iterator iter = name_score_map.begin(); iter != name_score_map.end(); ++iter) { cout << iter->first <<":"<<iter->second<<endl; //如果是map中interator,那么其必须使用->,如果是pair中,则使用点号 } */ /* pair <int, int> foo; pair <int, int> bar; foo = make_pair(10, 20); bar = make_pair(10.5, 'A'); // ok: implicit conversion from pair<double,char> 隐私转换 std::cout << "foo: " << foo.first << ", " << foo.second << '\n'; //foo:10,20 std::cout << "bar: " << bar.first << ", " << bar.second << '\n'; //bar:10,6 */ map<char, int> mymap; // first insert function version (single parameter): //这里面其实是有两个步骤,第一个步骤就是map中可以存储键值对,这里运用pair来构建键值对 //第二步:pair中可以使用make_pair()。也可以使用原本构造函数形式pair<char, int>('a', 100) mymap.insert(pair<char, int>('a', 100)); mymap.insert(pair<char, int>('z', 200)); //The single element versions return a pair //with its member pair::first set to an iterator pointing to either the newly inserted element or to the element with an equivalent key in the map. //The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent key already existed. pair<map<char, int>::iterator, bool> ret; ret = mymap.insert(pair<char, int>('z', 500)); if (ret.second == false) { std::cout << "element 'z' already existed"; std::cout << " with a value of " << ret.first->second << '\n'; //ret.first中对应的是一个 } // second insert function version (with hint position): map<char, int>::iterator it = mymap.begin(); mymap.insert(it, pair<char, int>('b', 300)); // max efficiency inserting mymap.insert(it, pair<char, int>('c', 400)); // no max efficiency inserting // third insert function version (range insertion): map<char, int> anothermap; anothermap.insert(mymap.begin(), mymap.find('c')); // showing contents:it->first输出map中的key,it->second输出的是map中value std::cout << "mymap contains:\n"; for (it = mymap.begin(); it != mymap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; std::cout << "anothermap contains:\n"; for (it = anothermap.begin(); it != anothermap.end(); ++it) std::cout << it->first << " => " << it->second << '\n'; /* map<char, int> mymap; map<char, int>::iterator it; mymap['a'] = 50; mymap['b'] = 100; mymap['c'] = 150; mymap['d'] = 200; //find返回的是An iterator to the element, 因此找到以后,我们可以使用mymap.find('a')->second。(interator就好比是一个poniter) //if an element with specified key is found, or map::end otherwise. it = mymap.find('b'); if (it != mymap.end()) mymap.erase(it);//删除该元素 // print content: std::cout << "elements in mymap:" << '\n'; std::cout << "a => " << mymap.find('a')->second << '\n'; std::cout << "c => " << mymap.find('c')->second << '\n'; std::cout << "d => " << mymap.find('d')->second << '\n'; */ return 0; }
相关文章推荐
- C++文件操作典型案例
- 【转】嵌入式C语言调试开关
- C语言中,头文件和源文件的关系(转)
- C++ sort 排序(降序、升序)使用总结
- DDX_Control 与 DDX_Text 区别
- 【VC++】没有与参数列表匹配的 重载函数 AfxMessageBox
- ClustLib——A C++ Data Clustering Framework(二):杂项
- C++中的指针
- C++ operator关键字(重载操作符)
- 【转】C++动态创建二维数组,二维数组指针
- C++经典"表达"
- [C++] 静态成员函数
- eclipse环境搭建之二:C++
- Win10+VC++下编译Libgeotiff(含Libtiff)详细图文教程
- ubuntu 安装 netbeans C++ IDE
- [C++] 多态性与虚函数
- C语言小结
- c++你不知道的用法之foreach篇
- 高精度加法(简明版C语言)
- C语言字符串操作总结大全(超详细)