当前位置: 首页 > news >正文

六站合一的优势wordpress 文章置顶

六站合一的优势,wordpress 文章置顶,做软件常用的网站有哪些软件,单位做好职工养老保险中断补缴的新闻STL 中map的用法详解 Map是STL的一个关联容器#xff0c;它提供一对一#xff08;其中第一个可以称为关键字#xff0c;每个关键字只能在map中出现一次#xff0c;第二个可能称为该关键字的值#xff09;的数据处理能力#xff0c;由于这个特性#xff0c;它完成有可能在…STL 中map的用法详解 Map是STL的一个关联容器它提供一对一其中第一个可以称为关键字每个关键字只能在map中出现一次第二个可能称为该关键字的值的数据处理能力由于这个特性它完成有可能在我们处理一对一数据的时候在编程上提供快速通道。这里说下map内部数据的组织map内部自建一颗红黑树(一种非严格意义上的平衡二叉树)这颗树具有对数据自动排序的功能所以在map内部所有的数据都是有序的。 下面举例说明什么是一对一的数据映射。比如一个班级中每个学生的学号跟他的姓名就存在着一一映射的关系这个模型用map可能轻易描述很明显学号用int描述姓名用字符串描述(本篇文章中不用char *来描述字符串而是采用STL中string来描述),下面给出map描述代码: Mapint, string mapStudent; 1.    map的构造函数 map共提供了6个构造函数这块涉及到内存分配器这些东西略过不表在下面我们将接触到一些map的构造方法这里要说下的就是我们通常用如下方法构造一个map Mapint, string mapStudent; 2.    数据的插入 在构造map容器后我们就可以往里面插入数据了。这里讲三种插入数据的方法 第一种:用insert函数插入pair数据下面举例说明(以下代码虽然是随手写的应该可以在VC和GCC下编译通过大家可以运行下看什么效果在VC下请加入这条语句屏蔽4786警告  pragma warning (disable:4786) ) #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent.insert(pairint, string(1, “student_one”));        mapStudent.insert(pairint, string(2, “student_two”));        mapStudent.insert(pairint, string(3, “student_three”));        mapint, string :: iterator  iter;        for(iter mapStudent.begin(); iter ! mapStudent.end(); iter) {            Coutiter - first ”   ” iter - second end; } } 第二种用insert函数插入value_type数据下面举例说明 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent.insert(mapint, string::value_type (1, “student_one”));        mapStudent.insert(mapint, string::value_type (2, “student_two”));        mapStudent.insert(mapint, string::value_type (3, “student_three”));        mapint, string::iterator  iter;        for(iter mapStudent.begin(); iter ! mapStudent.end(); iter) {            Coutiter-first”   ”iter-secondend; } } 第三种用数组方式插入数据下面举例说明 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent[1]   “student_one”;        mapStudent[2]   “student_two”;        mapStudent[3]   “student_three”;        mapint, string::iterator  iter;        for(iter mapStudent.begin(); iter ! mapStudent.end(); iter) {        Coutiter-first”   ”iter-secondend; } } 以上三种用法虽然都可以实现数据的插入但是它们是有区别的当然了第一种和第二种在效果上是完成一样的用insert函数插入数据在数据的插入上涉及到集合的唯一性这个概念即当map中有这个关键字时insert操作是插入数据不了的但是用数组方式就不同了它可以覆盖以前该关键字对应的值用程序说明 mapStudent.insert(mapint, string::value_type (1, “student_one”)); mapStudent.insert(mapint, string::value_type (1, “student_two”)); 上面这两条语句执行后map中1这个关键字对应的值是“student_one”第二条语句并没有生效那么这就涉及到我们怎么知道insert语句是否插入成功的问题了可以用pair来获得是否插入成功程序如下 Pairmapint, string::iterator,  bool Insert_Pair; Insert_Pair mapStudent.insert(mapint, string::value_type (1, “student_one”)); 我们通过pair的第二个变量来知道是否插入成功它的第一个变量返回的是一个map的迭代器如果插入成功的话Insert_Pair.second应该是true的否则为false。 下面给出完成代码演示插入成功与否问题 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent; Pairmapint, string::iterator, bool Insert_Pair;        Insert_Pair mapStudent.insert(pairint, string(1, “student_one”));        If(Insert_Pair.second true)        {               Cout”Insert Successfully”endl;        }        Else        {               Cout”Insert Failure”endl;        }        Insert_Pair mapStudent.insert(pairint, string(1, “student_two”));        If(Insert_Pair.second true)        {               Cout”Insert Successfully”endl;        }        Else        {               Cout”Insert Failure”endl;        }        mapint, string::iterator  iter;        for(iter mapStudent.begin(); iter ! mapStudent.end(); iter) {           Coutiter-first”   ”iter-secondend; } }   大家可以用如下程序看下用数组插入在数据覆盖上的效果 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent[1]   “student_one”;        mapStudent[1]   “student_two”;        mapStudent[2]   “student_three”;        mapint, string::iterator  iter;        for(iter mapStudent.begin(); iter ! mapStudent.end(); iter) {             Coutiter-first”   ”iter-secondend; } } 3. map的大小 在往map里面插入了数据我们怎么知道当前已经插入了多少数据呢可以用size函数用法如下 Int nSize mapStudent.size(); 4.  数据的遍历 这里也提供三种方法对map进行遍历 第一种应用前向迭代器上面举例程序中到处都是了略过不表 第二种应用反相迭代器下面举例说明要体会效果请自个动手运行程序 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent.insert(pairint, string(1, “student_one”));        mapStudent.insert(pairint, string(2, “student_two”));        mapStudent.insert(pairint, string(3, “student_three”));        mapint, string::reverse_iterator  iter;        for(iter mapStudent.rbegin(); iter ! mapStudent.rend(); iter) {           Coutiter-first”   ”iter-secondend; } } 第三种用数组方式程序说明如下 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent.insert(pairint, string(1, “student_one”));        mapStudent.insert(pairint, string(2, “student_two”));        mapStudent.insert(pairint, string(3, “student_three”));        int nSize mapStudent.size()        for(int nIndex 0; nIndex nSize; nIndex) {            CoutmapStudent[nIndex]end; } } 5.  数据的查找包括判定这个关键字是否在map中出现 在这里我们将体会map在数据插入时保证有序的好处。 要判定一个数据关键字是否在map中出现的方法比较多这里标题虽然是数据的查找在这里将穿插着大量的map基本用法。 这里给出三种数据查找方法 第一种用count函数来判定关键字是否出现其缺点是无法定位数据出现位置,由于map的特性一对一的映射关系就决定了count函数的返回值只有两个要么是0要么是1出现的情况当然是返回1了 第二种用find函数来定位数据出现位置它返回的一个迭代器当数据出现时它返回数据所在位置的迭代器如果map中没有要查找的数据它返回的迭代器等于end函数返回的迭代器程序说明 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent.insert(pairint, string(1, “student_one”));        mapStudent.insert(pairint, string(2, “student_two”));        mapStudent.insert(pairint, string(3, “student_three”));        mapint, string::iterator iter;        iter mapStudent.find(1); if(iter ! mapStudent.end()) {            Cout”Find, the value is ”iter-secondendl; } Else {            Cout”Do not Find”endl; } } 第三种这个方法用来判定数据是否出现是显得笨了点但是我打算在这里讲解 Lower_bound函数用法这个函数用来返回要查找关键字的下界(是一个迭代器) Upper_bound函数用法这个函数用来返回要查找关键字的上界(是一个迭代器) 例如map中已经插入了1234的话如果lower_bound(2)的话返回的2而upper-bound2的话返回的就是3 Equal_range函数返回一个pairpair里面第一个变量是Lower_bound返回的迭代器pair里面第二个迭代器是Upper_bound返回的迭代器如果这两个迭代器相等的话则说明map中不出现这个关键字程序说明 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent[1]   “student_one”;        mapStudent[3]   “student_three”;        mapStudent[5]   “student_five”;        mapint, string::iterator  iter; iter mapStudent.lower_bound(2); {        //返回的是下界3的迭代器        Coutiter-secondendl; } iter mapStudent.lower_bound(3); {        //返回的是下界3的迭代器        Coutiter-secondendl; }   iter mapStudent.upper_bound(2); {        //返回的是上界3的迭代器        Coutiter-secondendl; } iter mapStudent.upper_bound(3); {        //返回的是上界5的迭代器        Coutiter-secondendl; }   Pairmapint, string::iterator, mapint, string::iterator mapPair; mapPair mapStudent.equal_range(2); if(mapPair.first mapPair.second)       {        cout”Do not Find”endl; } Else { Cout”Find”endl;} mapPair mapStudent.equal_range(3); if(mapPair.first mapPair.second)       {        cout”Do not Find”endl; } Else { Cout”Find”endl;} } 6.       数据的清空与判空 清空map中的数据可以用clear()函数判定map中是否有数据可以用empty()函数它返回true则说明是空map 7.       数据的删除 这里要用到erase函数它有三个重载了的函数下面在例子中详细说明它们的用法 #include map #include string #include iostream Using namespace std; Int main() {        Mapint, string mapStudent;        mapStudent.insert(pairint, string(1, “student_one”));        mapStudent.insert(pairint, string(2, “student_two”));        mapStudent.insert(pairint, string(3, “student_three”));   //如果你要演示输出效果请选择以下的一种你看到的效果会比较好        //如果要删除1,用迭代器删除        mapint, string::iterator iter;        iter mapStudent.find(1);        mapStudent.erase(iter);          //如果要删除1用关键字删除        Int n mapStudent.erase(1);//如果删除了会返回1否则返回0          //用迭代器成片的删除        //一下代码把整个map清空        mapStudent.earse(mapStudent.begin(), mapStudent.end());        //成片删除要注意的是也是STL的特性删除区间是一个前闭后开的集合          //自个加上遍历代码打印输出吧 } 8.       其他一些函数用法 这里有swap,key_comp,value_comp,get_allocator等函数感觉到这些函数在编程用的不是很多略过不表有兴趣的话可以自个研究 9.       排序 这里要讲的是一点比较高深的用法了,排序问题STL中默认是采用小于号来排序的以上代码在排序上是不存在任何问题的因为上面的关键字是int型它本身支持小于号运算在一些特殊情况比如关键字是一个结构体涉及到排序就会出现问题因为它没有小于号操作insert等函数在编译的时候过不去下面给出两个方法解决这个问题 第一种小于号重载程序举例 #include map #include string Using namespace std; Typedef struct tagStudentInfo {        Int      nID;        String   strName; }StudentInfo, *PStudentInfo;  //学生信息   Int main() {     int nSize;        //用学生信息映射分数        mapStudentInfo, intmapStudent;     mapStudentInfo, int::iterator iter;        StudentInfo studentInfo;        studentInfo.nID 1;        studentInfo.strName “student_one”;        mapStudent.insert(pairStudentInfo, int(studentInfo, 90));        studentInfo.nID 2;        studentInfo.strName “student_two”; mapStudent.insert(pairStudentInfo, int(studentInfo, 80));   for (itermapStudent.begin(); iter!mapStudent.end(); iter)     coutiter-first.nIDendliter-first.strNameendliter-secondendl;   } 以上程序是无法编译通过的只要重载小于号就OK了如下 Typedef struct tagStudentInfo {        Int      nID;        String   strName;        Bool operator (tagStudentInfo const _A) const        {               //这个函数指定排序策略按nID排序如果nID相等的话按strName排序               If(nID _A.nID)  return true;               If(nID _A.nID) return strName.compare(_A.strName) 0;               Return false;        } }StudentInfo, *PStudentInfo;  //学生信息 第二种仿函数的应用这个时候结构体中没有直接的小于号重载程序说明 #include map #include string Using namespace std; Typedef struct tagStudentInfo {        Int      nID;        String   strName; }StudentInfo, *PStudentInfo;  //学生信息   Classs sort {        Public:        Bool operator() (StudentInfo const _A, StudentInfo const _B) const        {               If(_A.nID _B.nID) return true;               If(_A.nID _B.nID) return _A.strName.compare(_B.strName) 0;               Return false;        } };   Int main() {        //用学生信息映射分数        MapStudentInfo, int, sortmapStudent;        StudentInfo studentInfo;        studentInfo.nID 1;        studentInfo.strName “student_one”;        mapStudent.insert(pairStudentInfo, int(studentInfo, 90));        studentInfo.nID 2;        studentInfo.strName “student_two”; mapStudent.insert(pairStudentInfo, int(studentInfo, 80)); } 10.   另外 由于STL是一个统一的整体map的很多用法都和STL中其它的东西结合在一起比如在排序上这里默认用的是小于号即less如果要从大到小排序呢这里涉及到的东西很多在此无法一一加以说明。 还要说明的是map中由于它内部有序由红黑树保证因此很多函数执行的时间复杂度都是log2N的如果用map函数可以实现的功能而STL  Algorithm也可以完成该功能建议用map自带函数效率高一些。  转载于:https://www.cnblogs.com/xiaojinma/archive/2012/12/06/2805539.html
http://www.pierceye.com/news/345615/

相关文章:

  • 主域名进入网站广告标识标牌制作厂家
  • 网站建设基础流程摘要专题网站建设策划
  • 滁州网站建设电话网站建设与网站优化
  • 慈溪做网站公司哪家好淘宝商城的网站建设
  • 安徽建设厅网站怎么打不开太原网络搭建
  • idea 网站开发最好的免费推广平台
  • 专业排名优化网站怎么建网站教程视频app
  • 全国八大员报名官方网站支付宝小程序开发工具
  • 怎么查看vps网站服务器时间中国建设会计协会网站
  • 门户网站上的广告怎么做深圳服装网站建设
  • 公司网站上线的通知抚州营销型网站建设
  • 中国住房城乡和城乡建设部网站小广告文案
  • 做带字头像的网站wordpress 翻页设置
  • 网站横幅js代码公众号如何申请
  • 找网站建设需要问什么软件物联网平台功能
  • 含山县城市建设有限公司网站成都中高风险地区名单最新
  • 鄂州手机网站建设深圳seo网站设计
  • 网站内容的实现方式建筑设计人才招聘
  • 网站做优化效果怎样iis怎么做网站空间
  • 重庆市建设局网站郑州网站建设哪一家好
  • wordpress指定分类广告金融网站排名优化
  • 美妆网站建设方案陕西网络公司网站建设
  • 北京地铁建设的网站深圳建网站兴田德润可信
  • 平台门户网站建设企业战略规划方案
  • 北京网站备案的地址住房和城乡建设部网站质保金
  • 网络营销自学网站腾讯云服务器cvm
  • 建设旅行网站策划书浙江省职业能力建设处网站
  • 网站项目建设的组织机构wordpress做登录
  • 定制杯子岳阳优化营商环境
  • 中学院新校区建设专题网站如何搭建网络教学平台