STL的基本使用之关联容器:map和multiMap的基本使用
STL的基本使用之关联容器:map和multiMap的基本使用
简介
- map 和 multimap 内部也都是使用红黑树来实现,他们存储的是键值对,并且会自动将元素的key进行排序。两者不同在于map 不允许key重复,而multiSet 允许key重复
头文件 #include< map >
构造函数及析构函数

非变动性操作函数
- 运算符重载

- 下标运算符

- 查找操作函数

- 赋值操作

- 迭代器操作

- 运算符重载
插入删除操作

范例如下
#include <iostream>
#include <map>
#include <iomanip>
using namespace std;
int main ()
{ #pragma mark - 基本使用
map<int,string> c;
c.insert(make_pair(1,"1")); c.insert(make_pair(2,"2"));
c.insert(make_pair(4,"4")); c.insert(make_pair(5,"5"));
c.insert(make_pair(6,"6")); cout << "lower_bound(3): " << c.lower_bound(3)->second << endl;
cout << "upper_bound(3): " << c.upper_bound(3)->first << endl;
cout << "equal_range(3): "
<< (c.equal_range(3).first)->first << " "
<< (c.equal_range(3).second)->first << endl;
cout << endl;
cout << "lower_bound(5): " << c.lower_bound(5)->first << endl;
cout << "upper_bound(5): " << c.upper_bound(5)->first << endl;
cout << "equal_range(5): "
<< (c.equal_range(5).first)->first << " "
<< (c.equal_range(5).second)->first << endl; c.insert(c.lower_bound(3), make_pair(3, "3"));
// c.erase(4);
c.erase(c.find(4));
map<int,string>::iterator i;
for (i = c.begin(); i!=c.end(); i++) {
cout<<i->first<<" ";
}
cout<<endl; #pragma mark- 实例1:将map当做关联数组
typedef map<string,float> StringFloatMap;
StringFloatMap stocks; // create empty container //insert some elements
stocks["BASF"] = 369.50;
stocks["VW"] = 413.50;
stocks["Daimler"] = 819.00;
stocks["BMW"] = 834.00;
stocks["Siemens"] = 842.20; //print all elements
StringFloatMap::iterator pos;
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
cout << "stock: " << pos->first << "\t"
<< "price: " << pos->second << endl;
}
cout << endl;
//boom (all prices doubled)
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
pos->second *= 2;
} //print all elements
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
cout << "stock: " << pos->first << "\t"
<< "price: " << pos->second << endl;
}
cout << endl; /*rename key from "VW" to "Volkswagen"
*-only provided by exchanging element
*/
stocks["Volkswagen"] = stocks["VW"];
stocks.erase("VW"); //print all elements
for (pos = stocks.begin(); pos != stocks.end(); ++pos) {
cout << "stock: " << pos->first << "\t"
<< "price: " << pos->second << endl;
} #pragma mark - 实例2:将multimap当做字典
typedef multimap<string,string> StrStrMMap;
//create empty dictionary
StrStrMMap dict; //insert some elements in random order
dict.insert(make_pair("day","Tag"));
dict.insert(make_pair("strange","fremd"));
dict.insert(make_pair("car","Auto"));
dict.insert(make_pair("smart","elegant"));
dict.insert(make_pair("trait","Merkmal"));
dict.insert(make_pair("strange","seltsam"));
dict.insert(make_pair("smart","raffiniert"));
dict.insert(make_pair("smart","klug"));
dict.insert(make_pair("clever","raffiniert")); //print all elements
StrStrMMap::iterator pos1;
cout.setf (ios::left, ios::adjustfield);
cout << ' ' << setw(10) << "english "
<< "german " << endl;
cout << setfill('-') << setw(20) << ""
<< setfill(' ') << endl;
for (pos1 = dict.begin(); pos1 != dict.end(); ++pos1) {
cout << ' ' << setw(10) << pos1->first.c_str()
<< pos1->second << endl;
}
cout << endl; //print all values for key "smart"
string word("smart");
cout << word << ": " << endl; for (pos1 = dict.lower_bound(word);
pos1 != dict.upper_bound(word); ++pos1) {
cout << " " << pos1->second << endl;
} //print all keys for value "raffiniert"
word = ("raffiniert");
cout << word << ": " << endl;
for (pos1 = dict.begin(); pos1 != dict.end(); ++pos1) {
if (pos1->second == word) {
cout << " " << pos1->first << endl;
}
}
}
运行截图

STL的基本使用之关联容器:map和multiMap的基本使用的更多相关文章
- STL 笔记(二) 关联容器 map、set、multimap 和 multimap
STL 关联容器简单介绍 关联容器即 key-value 键值对容器,依靠 key 来存储和读取元素. 在 STL 中,有四种关联容器,各自是: map 键值对 key-value 存储,key 不可 ...
- STL的基本使用之关联容器:set和multiSet的基本使用
STL的基本使用之关联容器:set和multiSet的基本使用 简介 set 和 multiSet 内部都是使用红黑树来实现,会自动将元素进行排序.两者不同在于set 不允许重复,而multiSet ...
- C++关联容器<map>简单总结
C++关联容器<map>简单总结 map提供大小可变的关联容器,基于关联键值高效检索元素值.当你处理键值对的数据是,都可以考虑使用map关联容器. 特点: 大小可变的关联容器,基于关联键值 ...
- 关联容器——map、set
map类型通常被称为关联数组,与正常数组类似,不同之处在于其下标不必是整数.我们通过一个关键字而不是位置来查找值(键值对). 与之相对,set就是关键字的简单集合.当只是想知道一个值是否存在时,set ...
- c++中关联容器map的使用
C++关联容器<map>简单总结(转) 补充: 使用count,返回的是被查找元素的个数.如果有,返回1:否则,返回0.注意,map中不存在相同元素,所以返回值只能是1或0. 使用find ...
- C++ 之关联容器 map
标准库定义了四种关联容器:map是其中之一(另外还有set.multimap.multiset).map的元素以键-值(key-value),在学了顺序容器之后,再学习关联容器,就比较比较好理解了. ...
- 关联容器(map):支持高效查找的容器,一种键值对的集合。
#include <iostream> #include <string> #include <map> #include <vector> using ...
- STL标准库-容器-map和multimap
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 map与multimap为关联容器,结构如下 map底层实现依然是rb_tree 他的data可以改,但是key不能改,因此ma ...
- STL学习笔记— —容器map和multimap
简单介绍 在头文件<map> 中定义 namespace std { template <typename Key, typename T, typename Compare = l ...
随机推荐
- 我们说的oc是动态运行时语言是什么意思?
1.KVC和KVO区别,分别在什么情况下使用? 答:KVC(Key-Value-Coding) KVO(Key-Value-Observing)理解KVC与KVO(键-值-编码与键-值-监看) 当通 ...
- I/O CPU
http://www.educity.cn/zk/czxt/201306041038131789.htm http://blog.csdn.net/xiazdong/article/details/6 ...
- Milk Patterns
poj3261:http://poj.org/problem?id=3261 题意:给定一个字符串,求至少出现k 次的最长重复子串,这k 个子串可以重叠. 题解:还是用后缀数组,求H和后缀数组,然后二 ...
- zoj 3841 Cards
题意:给你52张牌,已知一个牌的序列,然后利用剩余的牌,能排成多少个序列,这个序列比已知的序列字典序小. 思路:从左到右尽可能放比已知序列相应位置小,找不到就放一样,然后求组合数就可以.多重集排列定理 ...
- error: /lib64/libc.so.6: symbol _dl_starting_up, version GLIBC_PRIVATE not defined in file ld-linux-x86-64.so.2 with link time reference
]$ sudo yum install libnotify*Loaded plugins: fastestmirror, refresh-packagekit, securitySetting up ...
- [LeetCode#256] Paint House
Problem: There are a row of n houses, each house can be painted with one of the three colors: red, b ...
- Linux Shell编程(26)——代码块重定向
像 while, until, 和 for 循环代码块, 甚至 if/then 测试结构的代码块都能做到标准输入的重定向. 即使函数也可以使用这种重定向的格式 .所有的这些依靠代码块结尾的 < ...
- Unity3d 真实的植物渲染
好久没写shader了,有些生疏,刚弄了个植物shader,分享一下. 先上图片: 重点需要注意的是fragment shader的透明部分 需要如此声明 Tags{ "LightMode& ...
- Hibernate(四)基本映射
映射的概念 在上次的博文Hibernate(三)Hibernate 配置文件我们已经讲解了一下 Hibernate中的两种配置文件,其中提到了两种配置文件的主要区别就是XML可以配置映射.这里提到的映 ...
- Windows玩转Docker(一):安装
Docker官网地址: http://www.docker.com/ 本文参照site: https://docs.docker.com/windows/ Docker 项目的目标是实现轻量级的操作系 ...