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 ...
随机推荐
- 自动生成makefile的脚本
如果需要测试某一个特性,写了一个test.cpp 某天又增加了一个utils.cpp,依此类推,测试文件越来越多 每次测试时都要手动维护一个makefile实在是不明智的 于是萌生了用脚本自动维护的念 ...
- matlab常用小函数(二)
numel 元素个数 assert 表达式为假时输出某个字符串 int2str 整形转化为字符串型 numel(A) 返回A中的元素个数,A可以是任何的数据结构,如向量.矩阵.元胞.结构体等 asse ...
- Uva_11722 Joining with Friend
题目链接 题意: 两个人坐火车, 在某个城市到站的时间段分别为[t1, t2] , [s1, s2],停在站台的时间均为w. 问, 若两人能见面的概率. 思路: 一道基础的几何概型, p = s(m) ...
- C#事件(Event)学习日记
event 关键字的来由,为了简化自定义方法的构建来为委托调用列表增加和删除方法. 在编译器处理 event 关键字的时候,它会自动提供注册和注销方法以及任何必要的委托类型成员变量. 这些委托成员变量 ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- Android开源项目发现--- 工具类数据库ORM篇(持续更新)
orm的db工具类,简化建表.查询.更新.插入.事务.索引的操作 1. greenDAO Android Sqlite orm的db工具类 项目地址:https://github.com/greenr ...
- java 中的访问修饰符
一. public:所有类都可以访问 protected:所有子类和同包下的类都可以访问 缺省:同包类都可以访问 private:类本身才可以访问 注意点:protected修饰类属性时,例如 pac ...
- bzoj1391
很像最大权闭合子图的题目s向每个工作连边,流量为收益每个工序,由工作i向对应机器连边,流量为租用费每个机器向t连边,流量为购买费显然跑最小割,ans=总收益-mincut ; type node=re ...
- 【转】Linux下svn的常用工作流程
原文网址:http://www.cnblogs.com/cobbliu/archive/2011/07/08/2389011.html 上篇文章在ubuntu和redhat5.5上搭建好了svnser ...
- 【转】更新SDK后,打包APK时报 the zipalign tool was not found in the sdk
原文网址:http://wyong.blog.51cto.com/1115465/1546632 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任 ...