C 中STL中的MAP用法详解
C++ STL----map

C++ STLBy Li-Bangzhu●map解释map(映射)——经过排序了的二元组的集合,map中的每个元素都是由两个值组成,其中的key(键值,一个map中的键值必须是唯一的)是在排序或搜索时使用,它的值可以在容器中重新获取;而另一个值是该元素关联的数值。
比如,除了可以ar[43] ="overripe"这样找到一个数据,map还可以通过ar["banana"] ="overripe"这样的方法找到一个数据。
如果你想获得其中的元素信息,通过输入元素的全名就可以轻松实现。
map一对一的映射的结合,key不能重复。
●map的使用//代码可直接运行,运行环境系统:centos 6.5//此段代码演示了几种不同方法的插入元素,遍历元素,查找,删除元素#include<iostream>#include<map>#include<string>using namespace std;int main(){map<int ,int> mymap; //此map的key和value 都是int 型的map<int ,int>::iterator it; //定义一个迭代子it = mymap.begin();//用数组的方式插入数据for(int i= 0;i<10;i++){mymap[i]= (i+3);}for(it = mymap.begin();it !=mymap.end();it++) //遍历所有元素{cout<<it->first<<" "<<it->second<<endl;}cout<<endl<<endl;//用insert 的方式插入数据for(int j=10;j<20;j++){mymap.insert(pair<int,int>(j,j+10));}for(it= mymap.begin();it != mymap.end();){cout<<it->first<<" "<<it->second<<endl; //遍历所有元素++it;}cout<<endl<<endl;//insert的另外一中方式插入元素for(int k =20; k<30 ;k++){mymap.insert(map<int,int>::value_type(k,k+100));}for(it = mymap.begin();it!=mymap.end();){cout<<it->first<<" "<<it->second<<endl;//遍历所有元素++it;}cout<<"this size of map"<<mymap.size()<<endl;for(int index =0;index<mymap.size();index++){cout<<mymap[index]<<" "; //遍历所有元素}cout<<endl;cout<<mymap[0]<<endl;cout<<mymap[27]<<endl; //随机读取//findit = mymap.find(27);if(it != mymap.end()){cout<<"find,this value"<<endl;//deletemymap.erase(it);}for(int index =0;index<mymap.size();index++){cout<<mymap[index]<<" ";}cout<<endl;//clearmymap.clear();cout<<"the size of mymap"<<mymap.size()<<endl; }更多详情请浏览个人文库:/p/helpylee3Q。
C++学习---STL常用容器之map容器

C++学习---STL常⽤容器之map容器8、map/multimap 容器8.1、map基本概念简介:map中所有元素都是pairpair中第⼀个元素为key(键值),起到索引作⽤,第⼆个元素为value(实值)所有元素都会根据元素的键值⾃动排序本质:map/multimap属于关联式容器,底层结构是⽤⼆叉树实现。
优点:可以根据key值快速找到value值map和multimap的区别:map不允许容器中有重复key值元素multimap允许容器中有重复key值元素8.2、map构造和赋值#include <iostream>#include <map>using namespace std;/*map<T1,T2> mp; //map默认构造函数map(const map &mp); //拷贝构造函数map& operator=(const map & mp); //重载等号操作符*///map容器构造和赋值void printMap(map<int, int> &m) {for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {cout << "key = " << (*it).first << " value = " << it->second << endl;}cout << endl;}void test01() {//创建map容器,默认构造map<int, int> m;//匿名对组放⼊容器中,默认按照key排序m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(6,60));m.insert(pair<int,int>(3,30));m.insert(pair<int,int>(4,40));printMap(m);//拷贝构造map<int, int> m2(m);printMap(m2);//赋值map<int, int> m3;m3 = m2;printMap(m3);}int main() {test01();system("pause");return0;}8.3、map⼤⼩和交换#include <iostream>#include <map>using namespace std;/*size(); //返回容器中元素的数⽬empty(); //判断容器是否为空swap(st); //交换两个集合容器*///map容器的⼤⼩和交换void printMap(map<int, int> &m) {for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {cout << "key:" << it->first << " value:" << it->second << endl; }cout << endl;}void test01() {map<int, int> m;m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(6,60));m.insert(pair<int,int>(3,30));m.insert(pair<int,int>(4,40));if (m.empty()) {cout << "m为空" << endl;}else {cout << "m不为空" << endl;}cout << "m的⼤⼩:" << m.size() << endl;}//交换void test02() {map<int, int> m1;m1.insert(pair<int,int>(1,10));m1.insert(pair<int,int>(2,20));m1.insert(pair<int,int>(3,30));map<int, int> m2;m2.insert(pair<int, int>(5, 500));m2.insert(pair<int, int>(6, 600));cout << "交换前:" << endl;printMap(m1);printMap(m2);m1.swap(m2);cout << "交换后:" << endl;printMap(m1);printMap(m2);}int main() {test01();test02();system("pause");return0;}8.4、map插⼊和删除#include <iostream>#include <map>using namespace std;/*insert(elem); //在容器中插⼊元素clear(); //清除所有元素erase(pos); //删除pos迭代器所指的元素,返回下⼀个元素的迭代器erase(beg,end);//删除区间[beg,end]的所有元素,返回下⼀个元素的迭代器erase(key); //删除容器中值为key的元素*///map容器的插⼊和删除void printMap(map<int, int> &m) {for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {cout << "key:" << it->first << " value:" << it->second << endl;}cout << endl;}void test01() {map<int, int> m;//第⼀种m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(6,60));//第⼆种m.insert(make_pair(2,20));//第三种m.insert(map<int,int>::value_type(3,30));//第四种,不建议使⽤[]插⼊m[5] = 40;//利⽤key有值的情况进⾏值的访问;//否则在key没有值的情况下,默认给值赋值为0,访问得到0cout << m[2] << endl;printMap(m);//删除m.erase(m.begin());printMap(m);//按照key删除,有则删除m.erase(3);printMap(m);//按照区间的⽅式删除,相当于清空//m.erase(m.begin(),m.end());m.clear();printMap(m);}int main() {test01();system("pause");return0;}8.5、map查找和统计函数原型:find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();count(key);//统计key的元素个数#include <iostream>#include <map>using namespace std;/*find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();count(key);//统计key的元素个数*///map容器的查找和统计void test01() {map<int, int> m;m.insert(pair<int,int>(1,10));m.insert(pair<int,int>(4,40));//map不允许插⼊重复的keym.insert(pair<int,int>(4,30));m.insert(pair<int,int>(2,20));map<int,int>::iterator pos = m.find(4);if (pos != m.end()) {cout << "查找到了元素 key=" << pos->first << " value=" << pos->second << endl; }else {cout << "未找到元素" << endl;}//统计,cout统计⽽⾔count统计⽽⾔,结果要么是 0,要么是1.//mutimap可以⼤于1int num = m.count(4);cout << "num = " << num << endl;}int main() {test01();system("pause");return0;}8.6、map容器排序利⽤仿函数可以改变排序规则#include <iostream>#include <map>using namespace std;/*map容器默认排序规则为按照key值进⾏从⼩到⼤排序利⽤仿函数可以改变排序规则,即重载了函数调⽤⼩括号*/class MyCompare {public:bool operator()(int v1, int v2){//降序return v1 > v2;}};void printMap(map<int, int, MyCompare> &m) {for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {cout << "key:" << it->first << " value:" << it->second << endl;}cout << endl;}//map容器的排序void test01() {//指定仿函数map<int, int, MyCompare> m;m.insert(pair<int,int>(1,10));m.insert(make_pair(2,20));m.insert(make_pair(5,50));m.insert(make_pair(3,30));printMap(m);}int main() {test01();system("pause");return0;}。
CSTL中Map的按Key排序和按Value排序

map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据k ey查到相应的val ue。
假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map 来进行存储就是个不错的选择。
我们这样定义,map<string, int>,其中学生姓名用stri ng 类型,作为Key;该学生的成绩用int类型,作为valu e。
这样一来,我们可以根据学生姓名快速的查找到他的成绩。
但是,我们除了希望能够查询某个学生的成绩,或许还想看看整体的情况。
我们想把所有同学和他相应的成绩都输出来,并且按照我们想要的顺序进行输出:比如按照学生姓名的顺序进行输出,或者按照学生成绩的高低进行输出。
换句话说,我们希望能够对map进行按Key排序或按V alue排序,然后按序输出其键值对的内容。
一、C++ STL中Ma p的按Ke y排序其实,为了实现快速查找,map内部本身就是按序存储的(比如红黑树)。
在我们插入<key, value>键值对时,就会按照ke y的大小顺序进行存储。
这也是作为k ey的类型必须能够进行<运算比较的原因。
现在我们用s tring类型作为k ey,因此,我们的存储就是按学生姓名的字典排序储存的。
【参考代码】1.#includ e<map>2.#includ e<string>3.#includ e<iostre am>ingnamesp ace std;5.6.typede f pair<string, int> PAIR;7.8.ostrea m& operat or<<(ostrea m& out, constPAIR& p) {9.return out << p.first<< "\t" << p.second;10.}11.12.int main() {13. map<string, int> name_s core_map;14. name_s core_map["LiMin"] = 90;15. name_s core_map["ZiLinM i"] = 79;16. name_s core_map["BoB"] = 92;17. name_s core_map.insert(make_p air("Bing",99));18. name_s core_map.insert(make_p air("Albert",86));19.for (map<string, int>::iterat or iter = name_s core_map.begin();20. iter != name_s core_map.end();21. ++iter) {22. cout << *iter << endl;23. }24.return 0;25. }【运行结果】大家都知道m ap是st l里面的一个模板类,现在我们来看下map的定义:1.templa te < classKey, classT, classCompar e = less<Key>,2.classAlloca tor = alloca tor<pair<constKey,T> > > classmap;它有四个参数,其中我们比较熟悉的有两个: Key 和 Value。
c++中的map的用法

c++中的map的用法Map是一种关联容器,它提供了一种映射的方式,将一个值(value)与一个键(key)相对应。
在C++ STL(Standard Template Library)中,Map是一个非常常用的容器类型,它具有很高的效率和灵活性。
Map内部使用红黑树(一种自平衡的二叉搜索树)来进行高效的查找和插入操作,它的不同之处在于,它的每个节点都包含了一个键值对,其中键是唯一的。
使用Map之前需要引入头文件<map>,其使用方法是:```c++#include <map>map<key_type, value_type> my_map;```key_type表示键的类型,value_type表示值的类型。
Map的常用操作包括:1. 插入Map的插入可以使用insert()函数,例如:```c++//插入键值对//使用数组下标插入my_map["Jerry"] = 20;```2. 查找Map的查找可以使用find()函数,例如:```c++//查找键为"Tom"的值map<string, int>::iterator it = my_map.find("Tom");if (it != my_map.end()) { //如果找到了cout << "Tom's age is " << it->second << endl; //输出Tom的年龄}```3. 删除Map的删除可以使用erase()函数。
例如:```c++//删除键为"Tom"的值my_map.erase("Tom");```4. 遍历Map的遍历可以使用迭代器,例如:```c++//遍历map中的所有键值对for (auto it = my_map.begin(); it != my_map.end(); it++) {cout << "key is " << it->first << " , value is " << it->second << endl;}```需要注意的是,Map中的元素是按照键的大小进行排序的,如果需要按照值的大小进行排序,可以使用multimap容器。
C++STL中哈希表hash_map从头到尾详细介绍

C++STL中哈希表hash_map从头到尾详细介绍0 为什么需要hash_map⽤过map吧?map提供⼀个很常⽤的功能,那就是提供key-value的存储和查找功能。
例如,我要记录⼀个⼈名和相应的存储,⽽且随时增加,要快速查找和修改:岳不群-华⼭派掌门⼈,⼈称君⼦剑张三丰-武当掌门⼈,太极拳创始⼈东⽅不败-第⼀⾼⼿,葵花宝典...这些信息如果保存下来并不复杂,但是找起来⽐较⿇烦。
例如我要找"张三丰"的信息,最傻的⽅法就是取得所有的记录,然后按照名字⼀个⼀个⽐较。
如果要速度快,就需要把这些记录按照字母顺序排列,然后按照⼆分法查找。
但是增加记录的时候同时需要保持记录有序,因此需要插⼊排序。
考虑到效率,这就需要⽤到⼆叉树。
讲下去会没完没了,如果你使⽤STL 的map容器,你可以⾮常⽅便的实现这个功能,⽽不⽤关⼼其细节。
关于map的数据结构细节,感兴趣的朋友可以参看。
看看map的实现:1 #include <map>2 #include <string>3using namespace std;4 ...5 map<string, string> namemap;6//增加。
7 namemap["岳不群"]="华⼭派掌门⼈,⼈称君⼦剑";8 namemap["张三丰"]="武当掌门⼈,太极拳创始⼈";9 namemap["东⽅不败"]="第⼀⾼⼿,葵花宝典";10 ...11//查找。
12if(namemap.find("岳不群") != namemap.end()){13 ...14 }不觉得⽤起来很easy吗?⽽且效率很⾼,100万条记录,最多也只要20次的pare的⽐较,就能找到你要找的记录;200万条记录事,也只要⽤21次的⽐较。
讲解有4种方式C++STLmapinsert()插入数据

讲解有4种⽅式C++STLmapinsert()插⼊数据前⾯讲过,C++ STL map 类模板中对[ ]运算符进⾏了重载,即根据使⽤场景的不同,借助[ ]运算符可以实现不同的操作。
举个例⼦:#include <iostream>#include <map> //map#include <string> //stringusing namespace std;int main(){std::map<string, string> mymap{ {"STL教程","http:///java/"} };//获取已存储键值对中,指定键对应的值cout << mymap["STL教程"] << endl;//向 map 容器添加新键值对mymap["Python教程"] = "http:///python/";//修改 map 容器已存储键值对中,指定键对应的值mymap["STL教程"] = "http:///stl/";for (auto iter = mymap.begin(); iter != mymap.end(); ++iter) {cout << iter->first << " " << iter->second << endl;}return 0;}程序执⾏结果为:http:///java/Python教程 http:///python/STL教程 http:///stl/可以看到,当操作对象为 map 容器中已存储的键值对时,则借助 [ ] 运算符,既可以获取指定键对应的值,还能对指定键对应的值进⾏修改;反之,若 map 容器内部没有存储以 [ ] 运算符内指定数据为键的键值对,则使⽤ [ ] 运算符会向当前 map 容器中添加⼀个新的键值对。
STL之六:mapmultimap用法详解

STL之六:mapmultimap⽤法详解STL中map数据结构1.map定义map是键-值对的集合。
map类型通常可以理解为关联数组:可使⽤键作为下标来获取⼀个值,正如内置数组类型⼀样。
⽽关联的本质在于元素的值与某个特定的键相关联,⽽并⾮通过元素在数组中的位置来获取。
<1>map模板原型:template < class Key, class T, class Compare = less<Key>, class Allocator = allocator<pair<const Key,T> > > class map;key:关键值的类型。
在map对象中的每个元素是通过该关键值唯⼀确定元素的。
T:映射值的类型。
在map中的每个元素是⽤来储存⼀些数据作为其映射值。
compare:Comparison类:A类键的类型,它有两个参数,并返回⼀个bool。
表达comp(A,B),comp是这⽐较类A和B是关键值的对象,应返回true,如果是在早先的⽴场⽐B放置在⼀个严格弱排序操作。
这可以是⼀个类实现⼀个函数调⽤运算符或⼀个函数的指针(见⼀个例⼦构造)。
默认的对于<KEY>,返回申请⼩于操作符相同的默认值(A <B)。
Map对象使⽤这个表达式来确定在容器中元素的位置。
以下这个规则在任何时候都排列在map容器中的所有元素。
Allocator:⽤于定义存储分配模型分配器对象的类型。
默认情况下,分配器类模板,它定义了最简单的内存分配模式,是值独⽴的<2>map模板参数map<Key, Data, Compare, Alloc><3>map的详细⽤法可参考:2.map的实现机制C++ STL 之所以得到⼴泛的赞誉,也被很多⼈使⽤,不只是提供了像vector, string, list等⽅便的容器,更重要的是STL封装了许多复杂的数据结构算法和⼤量常⽤数据结构操作。
C++ STL模板和Map使用大全

C++ STL模板和Map使用大全C++map的基本操作和使用(2009-09-23 14:58:21)分类:nguages标签:cmap编程基本操作livehaiitMap是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数;map<string , int >mapstring; map<int ,string >mapint;map<sring, char>mapstring; map< char ,string>mapchar;map<char ,int>mapchar; map<int ,char >mapint;2. map添加数据;map<int ,string> maplive;1.maplive.insert(pair<int,string>(102,"aclive"));2.maplive.insert(map<int,string>::value_type(321,"hai"));3, maplive[112]="April";//map中最简单最常用的插入添加!3,map中元素的查找:find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
map<int ,string >::iterator l_it;;l_it=maplive.find(112);if(l_it==maplive.end())cout<<"we do not find 112"<<endl;else cout<<"wo find 112"<<endl;4,map中元素的删除:如果删除112;map<int ,string >::iterator l_it;;l_it=maplive.find(112);if(l_it==maplive.end())cout<<"we do not find 112"<<endl;else maplive.erase(l_it); //delete 112;5,map中swap的用法:Map中的swap不是一个容器中的元素交换,而是两个容器交换; For example:#include <map>#include <iostream>using namespace std;int main( ){map <int, int> m1, m2, m3;map <int, int>::iterator m1_Iter;m1.insert ( pair <int, int> ( 1, 10 ) );m1.insert ( pair <int, int> ( 2, 20 ) );m1.insert ( pair <int, int> ( 3, 30 ) );m2.insert ( pair <int, int> ( 10, 100 ) );m2.insert ( pair <int, int> ( 20, 200 ) );m3.insert ( pair <int, int> ( 30, 300 ) );cout << "The original map m1 is:";for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )cout << " " << m1_Iter->second;cout << "." << endl;// This is the member function version of swap//m2 is said to be the argument map; m1 the target mapm1.swap( m2 );cout << "After swapping with m2, map m1 is:";for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )cout << " " << m1_Iter -> second;cout << "." << endl;cout << "After swapping with m2, map m2 is:";for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )cout << " " << m1_Iter -> second;cout << "." << endl;// This is the specialized template version of swapswap( m1, m3 );cout << "After swapping with m3, map m1 is:";for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )cout << " " << m1_Iter -> second;cout << "." << endl;}6.map的sort问题:Map中的元素是自动按key升序排序,所以不能对map用sort函数:For example:#include <map>#include <iostream>using namespace std;int main( ){map <int, int> m1;map <int, int>::iterator m1_Iter;m1.insert ( pair <int, int> ( 1, 20 ) );m1.insert ( pair <int, int> ( 4, 40 ) );m1.insert ( pair <int, int> ( 3, 60 ) );m1.insert ( pair <int, int> ( 2, 50 ) );m1.insert ( pair <int, int> ( 6, 40 ) );m1.insert ( pair <int, int> ( 7, 30 ) );cout << "The original map m1 is:"<<endl;for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ ) cout << m1_Iter->first<<" "<<m1_Iter->second<<endl;}The original map m1 is:1 202 503 604 406 407 30请按任意键继续. . .7, map的基本操作函数:C++ Maps是一种关联式容器,包含“关键字/值”对begin() 返回指向map头部的迭代器clear()删除所有元素count() 返回指定元素出现的次数empty() 如果map为空则返回trueend() 返回指向map末尾的迭代器equal_range() 返回特殊条目的迭代器对erase() 删除一个元素find() 查找一个元素get_allocator() 返回map的配置器insert() 插入元素key_comp() 返回比较元素key的函数lower_bound() 返回键值>=给定元素的第一个位置max_size() 返回可以容纳的最大元素个数rbegin() 返回一个指向map尾部的逆向迭代器rend() 返回一个指向map头部的逆向迭代器size() 返回map中元素的个数swap() 交换两个mapupper_bound() 返回键值>给定元素的第一个位置value_comp() 返回比较元素value的函数C++map的基本操作和使用(2009-09-23 14:58:21)分类:nguages标签:cmap编程基本操作livehaiitMap是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数;map<string , int >mapstring; map<int ,string >mapint;map<sring, char>mapstring; map< char ,string>mapchar;map<char ,int>mapchar; map<int ,char >mapint;2. map添加数据;map<int ,string> maplive;1.maplive.insert(pair<int,string>(102,"aclive"));2.maplive.insert(map<int,string>::value_type(321,"hai"));3, maplive[112]="April";//map中最简单最常用的插入添加!3,map中元素的查找:find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
int nSize = mapStudent.size()
for(int nIndex = 0; nIndex < nSize; nIndex++)
接触到一些 map 的构造方法,这里要说下的就是,我们通常用如下方法构造一个 map:
Map<int, string> mapStudent;
2.
数据的插入
在构造 map 容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:
第一种:用 insert 函数插入 pair 数据,下面举例说明(以下代码虽然是随手写的,应该可以
第一种:应用前向迭代器,上面举例程序中到处都是了,略过不表
Байду номын сангаас
第二种:应用反相迭代器,下面举例说明,要体会效果,请自个动手运行程序
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
Map<int, string> mapStudent;
STL 中 map 用法详解
说明:如果你具备一定的 C++ template 知识,即使你没有接触过 STL,这个文章你也应该可
能较轻易的看懂。本人水平有限,不当之处,望大家辅正。
一.Map 概述
Map 是 STL 的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能
在 map 中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它
{
Cout<<mapStudent[nIndex]<<end;
}
}
5.
数据的查找(包括判定这个关键字是否在 map 中出现)
在这里我们将体会,map 在数据插入时保证有序的好处。
要判定一个数据(关键字)是否在 map 中出现的方法比较多,这里标题虽然是数据的查找,
在这里将穿插着大量的 map 基本用法。
这里给出三种数据查找方法
第一种:用 count 函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于 map
的特性,一对一的映射关系,就决定了 count 函数的返回值只有两个,要么是 0,要么是 1,
mapStudent.insert(pair<int, string>(1, “student_one”));
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
}
第三种:用数组方式,程序说明如下
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one”));
#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main() {
Map<int, string> mapStudent; mapStudent.insert(map<int, string>::value_type (1, “student_one”)); mapStudent.insert(map<int, string>::value_type (2, “student_two”)); mapStudent.insert(map<int, string>::value_type (3, “student_three”)); map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { Cout<<iter->first<<” ”<<iter->second<<end; } } 第三种:用数组方式插入数据,下面举例说明
在着一一映射的关系,这个模型用 map 可能轻易描述,很明显学号用 int 描述,姓名用字符
串描述(本篇文章中不用 char *来描述字符串,而是采用 STL 中 string 来描述),下面给出 map
描述代码:
Map<int, string> mapStudent;
1.
map 的构造函数
map 共提供了 6 个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将
#include <map> #include <string> #include <iostream> Using namespace std; Int main() {
Map<int, string> mapStudent; mapStudent[1] = “student_one”; mapStudent[2] = “student_two”; mapStudent[3] = “student_three”; map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { Cout<<iter->first<<” ”<<iter->second<<end; } } 以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种 在效果上是完成一样的,用 insert 函数插入数据,在数据的插入上涉及到集合的唯一性这个 概念,即当 map 中有这个关键字时,insert 操作是插入数据不了的,但是用数组方式就不同 了,它可以覆盖以前该关键字对应的值,用程序说明
Cout<<”Insert Successfully”<<endl; } Else {
Cout<<”Insert Failure”<<endl; } map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { Cout<<iter->first<<” ”<<iter->second<<end; } } 大家可以用如下程序,看下用数组插入在数据覆盖上的效果
#include <map> #include <string> #include <iostream> Using namespace std; Int main() {
Map<int, string> mapStudent; mapStudent[1] = “student_one”; mapStudent[1] = “student_two”; mapStudent[2] = “student_three”; map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
mapStudent.insert(map<int, string>::value_type (1, “student_one”)); mapStudent.insert(map<int, string>::value_type (1, “student_two”)); 上面这两条语句执行后,map 中 1 这个关键字对应的值是“student_one”,第二条语句并没 有生效,那么这就涉及到我们怎么知道 insert 语句是否插入成功的问题了,可以用 pair 来获 得是否插入成功,程序如下
{
Cout<<iter->first<<” ”<<iter->second<<end;
}
}
3.
map 的大小
在往 map 里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用 size 函数,
用法如下:
Int nSize = mapStudent.size();
4.
数据的遍历
这里也提供三种方法,对 map 进行遍历
完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下 map 内部数
据的组织,map 内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据
自动排序的功能,所以在 map 内部所有的数据都是有序的,后边我们会见识到有序的好处。