关联容器------map与pair的使用
来源:互联网 发布:笔记本wifi搜不到网络 编辑:程序博客网 时间:2024/05/21 17:25
1、std::pair是二元组类模板,就是表示两种相互关联的事物,第一个叫做键,第二个叫做值,一个pair对象称为一个键值对。pair对象有两个变量,分别是first和second,调用first可以获取pair的键,调用second可以获取pair键所对应的值。我们既可以直接定义一个pair对象,也可以使用make_pair函数创建一个pair:
std::pair<char, int> a( 'a', 97 );make_pair( 'a', 97 );
2、map通俗的说就是一个二元组集合,里面存放的是pair这种二元组,但集合中任意两个pair的键是不能相同的。我们既可以通过map获取某个键所对应的值(通过下标的方式,其中键作为下标的索引),也可以通过pair来获取(pair->first = 键;pair->second = 值)。对于map的迭代器(iterator),对其解引用后,就是一个pair对象,所以iterator->first = 键、iterator->second = 值。
3、下面以一个例子来说明map和pair的定义、插入、以及键和值的获取等操作。
// map.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <map>using namespace std;int _tmain(int argc, _TCHAR* argv[]){map<int,string> map1;//map的定义1map1[1] = "beijing"; //通过下标插入值map1[2] = "shanghai";map1[3] = "tianjin";map1[4] = "chengde";map1[5] = "baoding";//通过下标输出cout<<map1[1].c_str()<<" "<<map1[2].c_str()<<" "<<map1[3].c_str()<<" "<<map1[4].c_str()<<" "<<map1[5].c_str()<<endl;//通过迭代器输出,迭代器解引用是一个pair类型的对象,//即迭代器是指向pair类型的指针map<int,string>::iterator iter = map1.begin();//map类型迭代器for (;iter != map1.end();iter++){cout<<(iter->second).c_str()<<" ";}cout<<endl;cout<<"----------------------------------------------------------------------"<<endl;map<int,string> map2(map1);//map的定义2//通过insert插入值,插入的是pair类型值map2.insert(map<int,string>::value_type(6,"shijiazhuang"));map2.insert(make_pair(7,"tangshan"));pair<int,string> p(8,"zhangjiakou");map2.insert(p);typedef map<int,string>::value_type pairType;map2.insert(pairType(9,"langfang"));map<int,string>::iterator iter2 = map2.begin();//map类型迭代器for (;iter2 != map2.end();iter2++){cout<<(iter2->second).c_str()<<" ";}cout<<endl;cout<<"----------------------------------------------------------------------"<<endl;map<int,string>::iterator iter3 = map2.begin();map<int,string>::iterator iter4 = map2.find(7);map<int,string> map3(iter3,iter4);//map的定义3pair<map<int,string>::iterator,bool> ret = map3.insert(make_pair(4,"cangzhou"));if (ret.second){cout<<"insert cang zhou success"<<endl;}else{cout<<"insert cang zhou error"<<"---";cout<<"相应键已经存在,其值为:"<<(ret.first->second).c_str()<<endl;}pair<map<int,string>::iterator,bool> ret1 = map3.insert(make_pair(10,"handan"));if (ret1.second){cout<<"insert handan success"<<endl;}else{cout<<"insert handan error"<<"---";cout<<"相应键已经存在,其值为:"<<(ret1.first->second).c_str()<<endl;}map<int,string>::iterator iter5 = map3.begin();for (;iter5 != map3.end();iter5++){cout<<(iter5->second).c_str()<<" ";}cout<<endl;return 0;}
阅读全文
0 0
- 关联容器------map与pair的使用
- 关联容器-map与pair的使用(续)
- STL之关联容器(pair、map、set的使用)
- STL之关联容器(pair、map、set的使用)
- map与pair的使用
- 【C/C++】关联容器pair map set
- C++:map容器和pair的结合使用
- 使用map关联容器
- set与map和pair容器
- C++:关联容器(pair、map、set、multiset)
- 关联容器(map、set、multimap、multiset、pair、unordered_map)
- 【STL容器学习】-关联容器与map的使用方法
- 顺序容器vector 与 关联容器map的总结
- STL map的遍历与pair的使用
- set/multiset,map/multimap的使用与比较+pair介绍
- C++ STL标准库的关联容器set与map
- STL 关联式容器 Set与Map的用法
- 使用map关联容器实现单词转换的程序
- Http Resources
- hbase常识及habse适合什么场景 (总结5)
- 硕士生预答辩中存在的共性问题
- Lua_ uLua_C#调用Lua方法_019
- js实现表格的增删改查
- 关联容器------map与pair的使用
- Idea导出jar包
- android一键清除最近应用按钮
- 哈哈日语养成十个习惯让你“疯狂说日语”
- Android样式的开发:View Animation篇
- C语言开发中应该注意的一些问题
- C中表达式语句以及副作用和++,--的有关问题
- 自定义View:Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context,
- lcs 51nod 1006 最长公共子序列Lcs