cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器
map(映射,key不能重复,一对一对的,value_type(1, "one")),multimap(多映射key可以重复)
红黑树(数据结构)map,multimap就是红黑树-二叉树
基本操作
insert:4 种方法
count和find
erase:3种方法
注意:不能通过find进行修改。

a.insert(map<int, string>::value_type(1, "one")); 数值1就是key键,"one"就是值。就是一对

map 与 multimap是存储key-value(键-值 对)类型的容器。

不同之处在于:map只允许key与 value一一对应;multimap一个key可对应多个value;

STL在线手册英文链接 :http://www.cplusplus.com/reference/stl/

STL在线手册中文链接 :http://c.biancheng.net/stl/

txwtech@163.com

 /*cb22a_c++_标准模板库_STL_map_multimap红黑树(数据结构)关联容器
map(映射,key不能重复,一对一对的,value_type(1, "one")),multimap(多映射key可以重复)
红黑树(数据结构)map,multimap就是红黑树-二叉树
基本操作
insert:4 种方法
count和find
erase:3种方法
注意:不能通过find进行修改。 a.insert(map<int, string>::value_type(1, "one")); 数值1就是key键,"one"就是值。就是一对 map 与 multimap是存储key-value(键-值 对)类型的容器。 不同之处在于:map只允许key与 value一一对应;multimap一个key可对应多个value; STL在线手册英文链接 :http://www.cplusplus.com/reference/stl/ STL在线手册中文链接 :http://c.biancheng.net/stl/
*/
#include <iostream>
#include <map>
#include <string> using namespace std; int main()
{
map<int, string> a;
map<string, int> score;
multimap<int, string> ma; cout << "插入数据" << endl;
a.insert(map<int, string>::value_type(, "one"));//数值1就是键,"one"就是值。就是一对
a.insert(map<int, string>::value_type(, "two"));
a.insert(map<int, string>::value_type(, "Three"));
a.insert(make_pair(-, "Minus One"));//插入方法2
a.insert(pair<int, string>(, "One Thousand"));//插入方法3
a[] = "One Million";//插入方法4,不能用于multimap score.insert(make_pair("scott", ));
score.insert(make_pair("sunny", ));
score.insert(make_pair("Gates", ));
score["bill"] = ;
cout << "bill score is:" << score["bill"] << endl;
cout << "Gates score is: " << score["Gates"] << endl; cout << "map查找返回的是一个常迭代器" << endl;
cout << a[] << endl;//下标3对应three;
cout << a[-] << endl;//-1,对应--Minus One cout << "map里面里面一共有:" << a.size() << " 个键值对数据";
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(, "Three"));
ma.insert(multimap<int, string>::value_type(, "Forty Five"));
ma.insert(make_pair(-, "Minus one1"));
ma.insert(pair<int, string>(, "One Thousand"));
ma.insert(pair<int, string>(, "one Thousand")); cout << endl << "multimap" << ma.size() << " 个数据。" << endl;
cout << "multimap数据显示:" << 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 << " value:" << im->second.c_str();
cout << endl;
}
cout << "multimap有" << ma.count() << " 个1000" << endl; cout << "multimap查找返回的是一个常迭代器" << endl;
multimap<int, string>::const_iterator fi; fi=ma.find();
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();
for (size_t i = ; i < n; ++i)
{
cout << "\t Key: " << fi->first;
cout << ", Value[" << i << "]=";
cout << fi->second << endl;
++fi;
}
} cout << "erase删除方法的使用" << endl;
if (ma.erase(-) > )
cout << "通过key删除成功,结果大于0:" << endl; cout << "通过查找,再删除" << endl; multimap<int, string>::iterator iElementFound = ma.find();
if (iElementFound != ma.end())
{
ma.erase(iElementFound);
cout << "删除45成功咯" << endl;
} ma.erase(ma.lower_bound(), ma.upper_bound());
cout << "从第一个1000,到最后一个1000的数据都删除了" << endl; return ;
}

最新文章

  1. WebViewJavascriptBridge源码探究--看OC和JS交互过程
  2. mailto
  3. 理解MySQL数据库覆盖索引
  4. C#GDI+图像处理
  5. ubuntu MySQL采用apt-get install安装目录情况
  6. Python进阶04 函数的参数对应
  7. EXTJS 4.2 添加滚动条
  8. C语言学习笔记--类型定义&amp;联合
  9. Fast portable non-blocking network programming with Libevent
  10. PyQt5+python3+pycharm开发环境配置
  11. Python基础3切片,字符串的方法
  12. C# 绘制PDF嵌套表格
  13. rancher的Ingress的文件大小上传限制配置
  14. Glide高级详解—缓存与解码复用
  15. C# 验证给定的字符串形式的日期是否合法
  16. 字符集更改步骤,mysql乱码
  17. eclipse gradle插件 org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from &#39;https://services.gradle.org/distributions/gradle-3.4-bin.zip&#39;.
  18. FileOutputSteam入门
  19. 大数据开发实战:Stream SQL实时开发一
  20. Scala的下载和安装(本地)

热门文章

  1. vue仿移动端输入框
  2. 关于docker的常见使用命令
  3. 计算机启动 Ubuntu系统初始化 SysV Systemd
  4. 前端星计划笔记-day1
  5. 【C++】常量
  6. 选择器&amp;隔行换色
  7. Java实现 LeetCode 836 矩形重叠(暴力)
  8. Java实现桐桐的数学难题
  9. Java实现 LeetCode 324 摆动排序 II
  10. Java实现 蓝桥杯VIP 算法提高 分苹果