建站公司如何在抖音平台开店,数据库做后台网站,怎样创作一个网站,网站建设文化渠道Maps是一种关联式容器#xff0c;包含“关键字/值”对。 Multimaps和maps很相似#xff0c;但是MultiMaps允许重复的元素。 简单介绍#xff1a; 1、声明#xff0c;首先包含头文件 “map” map int,string test1,test2;//map int,string::iterator it1,it…Maps是一种关联式容器包含“关键字/值”对。 Multimaps和maps很相似但是MultiMaps允许重复的元素。 简单介绍 1、声明首先包含头文件 “map” map int,string test1,test2;//map int,string::iterator it1,it2;//迭代器multimap int,string test3;multimap int,string::iterator it3; 2、插入数据可使用三种方法 第一种使用pair函数 test1.insert(pairint,string(1,song));test1.insert(pairint,string(2,zhang));test1.insert(pairint,string(3,wang)); 第二种使用value_type类型 test1.insert(mapint,string::value_type(4,qian));test1.insert(mapint,string::value_type(5,sun)); 第三种使用数组方式,可以覆盖原来数据前两中不能改变数据如果存在则插入失败 test1[6] mao;test1[7] guang; 前两种情况插入失败后可以通过以下方法检查 //测试是否插入成功pairmapint,string::iterator,bool insert_Pair;insert_Pair test1.insert(pairint,string(1,Replace));if (insert_Pair.second true){coutinsert successfullyendl;}else{coutinsert failureendl;} 3、遍历数据可使用以下几种方法 第一正向遍历 for (it1 test1.begin();it1 ! test1.end();it1){coutit1-first----- it1-secondendl;} 第二逆向遍历使用反向迭代器 cout反向迭代器endl;//rbegin()指向最后一个元素rend()指向第一个元素前面这里是指往前走一个位置mapint,string::reverse_iterator reverseIte;for (reverseIte test1.rbegin();reverseIte ! test1.rend();reverseIte){coutreverseIte-first----- reverseIte-secondendl;} 第三使用数组进行遍历 //使用数组方式进行遍历for (int i 1;i test1.size();i){couti-----test1[i]endl;} 4、查找和判断 第一使用count进行判断 //count判断int i1;for (it1 test1.begin();it1 ! test1.end();i,it1){couti;if (test1.count(i)0)//元素存在{coutis a element of mapendl;}else{coutis not a element of mapendl;}} 第二使用find判断 it2 test1.find(6);//查找if (it2 ! test1.end()){coutfind it:it2-first---it2-secondendl;}else{coutnot find itendl;} 5、删除元素 //删除元素it2 test1.find(2);test1.erase(it2); 这些操作基本上都和set的差不多好多函数一模一样。 转载于:https://www.cnblogs.com/songliquan/p/3341210.html