C 中STL中的MAP用法详解
- 格式:pdf
- 大小:123.02 KB
- 文档页数:9
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容器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;}。
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的用法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从头到尾详细介绍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()插⼊数据前⾯讲过,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中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++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尾部的迭代器。
C++中的STL中map⽤法详解Map是STL的⼀个关联容器,它提供⼀对⼀(其中第⼀个可以称为关键字,每个关键字只能在map中出现⼀次,第⼆个可能称为该关键字的值)的数据处理能⼒,由于这个特性,它完成有可能在我们处理⼀对⼀数据的时候,在编程上提供快速通道。
这⾥说下map内部数据的组织,map内部⾃建⼀颗红⿊树(⼀种⾮严格意义上的平衡⼆叉树),这颗树具有对数据⾃动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。
1、map简介map是⼀类关联式容器。
它的特点是增加和删除节点对迭代器的影响很⼩,除了那个操作节点,对其他的节点都没有什么影响。
对于迭代器来说,可以修改实值,⽽不能修改key。
2、map的功能⾃动建⽴Key-value的对应。
key 和 value可以是任意你需要的类型。
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
快速插⼊Key -Value 记录。
快速删除记录根据Key 修改value记录。
遍历所有记录。
3、使⽤map使⽤map得包含map类所在的头⽂件#include <map> //注意,STL头⽂件没有扩展名.hmap对象是模板类,需要关键字和存储对象两个模板参数:std:map<int,string> personnel;这样就定义了⼀个⽤int作为索引,并拥有相关联的指向string的指针.为了使⽤⽅便,可以对模板类进⾏⼀下类型定义,typedef map<int,CString> UDT_MAP_INT_CSTRING;UDT_MAP_INT_CSTRING enumMap;4、map的构造函数map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下⾯我们将接触到⼀些map的构造⽅法,这⾥要说下的就是,我们通常⽤如下⽅法构造⼀个map:map<int, string> mapStudent;5、数据的插⼊在构造map容器后,我们就可以往⾥⾯插⼊数据了。
STL中map的用法剖析摘要本文深入剖析了C++标准模板库(STL)中的map,对其概念和用法进行了深入探讨,并结合实例,详细阐述了map的相关用法。
关键词STL;map;插入;删除;排序1 map 概述STL(Standard Template Library 标准模版库)是C++标准程序库的核心,它深刻影响了标准程序库的整体结构。
STL是一个范型(generic)程序库,提供一系列软件方案,利用先进、高效的算法来管理数据。
STL 的好处在于封装了许多数据结构和算法(algorithm),map就是其典型代表。
map是STL的一个关联容器,它提供一对一(key/value 其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可以称为该关键字的值)的数据处理能力,由于这个特性,在处理一对一数据的时候,可以提供编程的快速通道。
2 map 的用法假设一个班级中,每个学生的学号和他的姓名存在一一映射的关系,这个模型用map 可以轻易描述,学号用int 描述,姓名用字符串描述,给出map 的描述代码:map<int,string> mapStudent 。
2.1 插入数据map元素的插入功能可以通过以下操作实现:第一种通过主键获取map中的元素,如果获取到,则返回对应结点对应的实值(存储在map 结点中的对象)。
但这个方法会产生副作用,如果以主键“key”获取结点的实值,在map 中并不存在这个结点,则会直接向map 中插入以key 为主键的结点,并返回这个结点,这时可以对其进行赋值操作。
但如果在map 中存在了以key 为主键的结点,则会返回这个结点的实值,如果此时进行复制操作,则会出现原来结点被新结点覆盖的危险,如果是指针类型则会出现内存泄漏等问题。
由于存在这样的副作用,不建议使用这种方法进行元素的插入。
第二种插入value_type数据。
insert 方法接口原型:pair<ierator,bool> insert(constvalue_type& X)该方法需要构建一个键值对,即value_type,然后调用insert方法,在该方法中实现根据键值对中的key值查找对应的结点,如果查找到,则不插入当前结点,并返回找到的那个结点,并将pair 中的第二个量置为false;否则插入当前结点,并返回插入的当前结点,且第二个值置为true。