C++之STL(6)之 map 与 multimap 关联容器
2016-07-27 00:03
513 查看
#include<iostream> #include<map> #include<string> using namespace std; int main() { map<int, string> a; multimap<int, string> ma; // 插入 3种方法 key键 value 值 a.insert(map<int, string>::value_type(1, "One")); a.insert(map<int, string>::value_type(2, "Two")); a.insert(map<int, string>::value_type(3, "Three")); a.insert(make_pair(-1, "Minus One")); a.insert(pair<int, string>(1000, "One Thousand")); a[1000000] = "One Million"; cout << "map里一共有" << a.size() << "个key-value对数据"; cout << "这些数据是:" << endl; map<int, string>::const_iterator i; for (i = a.begin(); i != a.end(); ++i) { cout << "Key:" << i->first; cout << "Value:" << i->second.c_str(); cout << endl; } ma.insert(multimap<int, string>::value_type(3, "Three")); ma.insert(multimap<int, string>::value_type(45, "Forty Five")); ma.insert(make_pair(-1, "Minus One")); ma.insert(pair<int, string>(1000, "One Thousand")); ma.insert(pair<int, string>(1000, "One Thousand")); cout << endl << "multimap里面有" << ma.size() << "个数" << endl; multimap<int, string>::const_iterator im; for (im = ma.begin(); im != ma.end(); im++) { cout << "Key:" << im->first; cout << "Value:" << im->second.c_str(); cout << endl; } cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl; system("pause"); return 0; }
总结:
3种插入方法:
a.insert(map<int, string>::value_type(1, "One"));
a.insert(make_pair(-1, "Minus One"));
a.insert(pair<int, string>(1000, "One Thousand"));count方法
cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl;
#include<iostream> #include<map> #include<string> using namespace std; int main() { map<int, string> a; map<string, int> m_Score; multimap<int, string> ma; // 插入 3种方法 key键 value 值 a.insert(map<int, string>::value_type(1, "One")); a.insert(map<int, string>::value_type(2, "Two")); a.insert(map<int, string>::value_type(3, "Three")); a.insert(make_pair(-1, "Minus One")); a.insert(pair<int, string>(1000, "One Thousand")); a[1000000] = "One Million"; m_Score.insert(make_pair("张飞", 99)); m_Score.insert(make_pair("刘备", 56)); m_Score["关羽"] = 87; // 对map进行查找 cout << "最简单的查找:" << endl; cout << m_Score["刘备"] << endl; cout << a[3] << endl; cout << "map里一共有" << a.size() << "个key-value对数据"; cout << "这些数据是:" << endl; map<int, string>::const_iterator i; for (i = a.begin(); i != a.end(); ++i) { cout << "Key:" << i->first; cout << "Value:" << i->second.c_str(); cout << endl; } ma.insert(multimap<int, string>::value_type(3, "Three")); ma.insert(multimap<int, string>::value_type(45, "Forty Five")); ma.insert(make_pair(-1, "Minus One")); ma.insert(pair<int, string>(1000, "One Thousand")); ma.insert(pair<int, string>(1000, "One Thousand")); cout << endl << "multimap里面有" << ma.size() << "个数" << endl; multimap<int, string>::const_iterator im; for (im = ma.begin(); im != ma.end(); im++) { cout << "Key:" << im->first; cout << "Value:" << im->second.c_str(); cout << endl; } cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl; // 查找 multimap<int, string>::iterator fi; // 不能通过迭代器进行修改 fi = ma.find(45); if (fi != ma.end()) { cout << "找到了!" << fi->first << "=" << fi->second.c_str() << endl;; } else cout << "没有找到!" << endl; // 找重复的 fi = ma.find(1000); if (fi != ma.end()) { cout << "找到了1000!" << endl; size_t n = ma.count(1000); for (size_t i = 0; i < n; i++) { cout << "\t Key:" << fi->first; cout << ", Value [" << i << "] = "; cout << fi->second.c_str() << endl; ++fi; } } else { cout << "没找到1000!" << endl; } system("pause"); return 0; }
#include<iostream> #include<map> #include<string> using namespace std; int main() { map<int, string> a; map<string, int> m_Score; multimap<int, string> ma; // 插入 3种方法 key键 value 值 a.insert(map<int, string>::value_type(1, "One")); a.insert(map<int, string>::value_type(2, "Two")); a.insert(map<int, string>::value_type(3, "Three")); a.insert(make_pair(-1, "Minus One")); a.insert(pair<int, string>(1000, "One Thousand")); a[1000000] = "One Million"; m_Score.insert(make_pair("张飞", 99)); m_Score.insert(make_pair("刘备", 56)); m_Score["关羽"] = 87;// 这个方法是用的最多的 // 对map进行查找 cout << "最简单的查找:" << endl; cout << m_Score["刘备"] << endl; cout << a[3] << endl; cout << "map里一共有" << a.size() << "个key-value对数据"; cout << "这些数据是:" << endl; map<int, string>::const_iterator i; for (i = a.begin(); i != a.end(); ++i) { cout << "Key:" << i->first; cout << "Value:" << i->second.c_str(); cout << endl; } ma.insert(multimap<int, string>::value_type(3, "Three")); ma.insert(multimap<int, string>::value_type(45, "Forty Five")); ma.insert(make_pair(-1, "Minus One")); ma.insert(pair<int, string>(1000, "One Thousand")); ma.insert(pair<int, string>(1000, "One Thousand")); cout << endl << "multimap里面有" << ma.size() << "个数" << endl; multimap<int, string>::const_iterator im; for (im = ma.begin(); im != ma.end(); im++) { cout << "Key:" << im->first; cout << "Value:" << im->second.c_str(); cout << endl; } cout << "multimap里面有" << ma.count(1000) << "个1000!" << endl; // 查找 multimap<int, string>::iterator fi; // 不能通过迭代器进行修改 fi = ma.find(45); if (fi != ma.end()) { cout << "找到了!" << fi->first << "=" << fi->second.c_str() << endl;; } else cout << "没有找到!" << endl; // 找重复的 fi = ma.find(1000); if (fi != ma.end()) { cout << "找到了1000!" << endl; size_t n = ma.count(1000); for (size_t i = 0; i < n; i++) { cout << "\t Key:" << fi->first; cout << ", Value [" << i << "] = "; cout << fi->second.c_str() << endl; ++fi; } } else { cout << "没找到1000!" << endl; } // 删除方法 if (ma.erase(-1) > 0) { cout << "删除成功-1!" << endl; } // 删除方法 multimap<int, string>::iterator iEllementFound = ma.find(45); if (iEllementFound != ma.end()) { ma.erase(iEllementFound); cout << "删除45成功" << endl; } // 删除方法 ma.erase(ma.lower_bound(1000), ma.upper_bound(1000));// 删除一个范围 system("pause"); return 0; }
相关文章推荐
- C语言空指针总结
- 【学习笔记】 C++里面的绑定类型
- Cpp Primer - constexpr
- 设计模式总结2
- 一起talk C栗子吧(第一百七十八回:C语言实例--字符和字符串输出函数一)
- 李洪强漫谈iOS开发[C语言-024]-表达式与赋值运算符
- C++移位运算符
- Leetcode 48. Rotate Image (Medium) (cpp)
- <C++略识>之重载、覆盖、隐藏
- 337. House Robber III beats 76.16% 12ms cpp
- C语言中的参数不确定
- 《C++代码设计与重用》读书笔记
- 设计模式总结01----设计 模式中类的关系
- C/C++内存泄漏及检测
- C++封装POSIX 线程库(三)线程的封装
- C++中endl、“\n”和‘\n’的区别
- 李洪强漫谈iOS开发[C语言-023]-取余数运算符
- 进阶项目3-点结构体
- C++虚函数和纯虚函数
- 进阶项目2.1-学生成绩处理