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 ...
随机推荐
- golang中设置Host Header的小Tips
前言 笔者最近时间一直在学习和写Ruby和Go,尤其是Go,作为云计算时代的标准语言,写起来还是相当有感觉的,难过其会越来越火. 不过写的过程中,也遇到了一些小问题,本文就是分享关于go语言设置 HT ...
- common头文件
#ifndef COMMON_HHH #define COMMON_HHH #define ASSERT(p) \ do{\ if (!p){\ printf("%s:%d\n", ...
- 【转载】利用jetty+Eclipse实现超轻量级web开发
之前一直使用maven的jetty插件.今天换种方式. 使用下面介绍的方式你只有一个java project就行. 开发环境更简单,debug也更方便,不需要remote debug的方式,jetty ...
- android的原理,为什么不需要手动关闭程序
转自android的原理,为什么不需要手动关闭程序 不用在意剩余内存的大小,其实很多人都是把使用其他系统的习惯带过来来了. Andoird大多应用没有退出的设计其实是有道理的,这和系统对进程的调度机制 ...
- 树莓派学习路程No.1 GPIO功能初识 wiringPi安装
WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi遵守GUN Lv3.wiringPi使用C或者C++开发并且可以被其他语言包转,例如python.ruby或者PHP等.Wiri ...
- 我所理解的OOP——UML六种关系(转)
转自:http://www.cnblogs.com/dolphinX/p/3296681.html 最近由于经常给公司的小伙伴儿们讲一些OOP的基本东西,每次草纸都被我弄的很尴尬,画来画去自己都乱了, ...
- VC6.0 list sort出错
在STL中,排序是个很重要的话题. 1.algorithm 里的sort()只接收RandomAccessIterator用于像vector,dequeue的排序 2.像set,map,这种关联式容器 ...
- 【HDOJ】2988 Dark roads
最小生成树. /* */ #include <iostream> #include <string> #include <map> #include <que ...
- 【动态规划】天堂(Heaven) 解题报告
天堂(heaven) 题目描述 每一个要上天堂的人都要经历一番考验,当然包括小X,小X开始了他进入天堂的奇异之旅.地狱有18层,天堂竟然和地狱一样,也有很多很多层,天堂共有N层.从下到上依次是第1,2 ...
- Ural 1258 镜面对称
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #inclu ...