zoukankan      html  css  js  c++  java
  • c++ STL map 结构体

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。

    在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题

    第一种:小于号重载,程序举例

    #include <map>

    #include <string>

    Using namespace std;

    Typedef struct tagStudentInfo

    {

           Int      nID;

           String   strName;

    }StudentInfo, *PStudentInfo;  //学生信息

     

    Int main()

    {

        int nSize;

           //用学生信息映射分数

           map<StudentInfo, int>mapStudent;

        map<StudentInfo, int>::iterator iter;

           StudentInfo studentInfo;

           studentInfo.nID = 1;

           studentInfo.strName = “student_one”;

           mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

           studentInfo.nID = 2;

           studentInfo.strName = “student_two”;

    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

     

    for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)

        cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;

     

    }

    以上程序是无法编译通过的,只要重载小于号,就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()

    {

           //用学生信息映射分数

           Map<StudentInfo, int, sort>mapStudent;

           StudentInfo studentInfo;

           studentInfo.nID = 1;

           studentInfo.strName = “student_one”;

           mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));

           studentInfo.nID = 2;

           studentInfo.strName = “student_two”;

    mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

    }

    1. /****************************************************************** 
    2.       map的基本操作函数: 
    3.       C++ Maps是一种关联式容器,包含“关键字/值”对 
    4.       begin()          返回指向map头部的迭代器 
    5.       clear()         删除所有元素 
    6.       count()          返回指定元素出现的次数 
    7.       empty()          如果map为空则返回true 
    8.       end()            返回指向map末尾的迭代器 
    9.       equal_range()    返回特殊条目的迭代器对 
    10.       erase()          删除一个元素 
    11.       find()           查找一个元素 
    12.       get_allocator()  返回map的配置器 
    13.       insert()         插入元素 
    14.       key_comp()       返回比较元素key的函数 
    15.       lower_bound()    返回键值>=给定元素的第一个位置 
    16.       max_size()       返回可以容纳的最大元素个数 
    17.       rbegin()         返回一个指向map尾部的逆向迭代器 
    18.       rend()           返回一个指向map头部的逆向迭代器 
    19.       size()           返回map中元素的个数 
    20.       swap()            交换两个map 
    21.       upper_bound()     返回键值>给定元素的第一个位置 
    22.       value_comp()      返回比较元素value的函数
    1. ==================================================================== 
    2. 1、map构造 
    3.     map<int, string> mapStudent; 
    4.  
    5. 2、map添加数据 
    6.     mapStudent.insert(pair<int, string>(1, "student_one")); 
    7.     mapStudent.insert(map<int, string>::value_type(2, "student_two")); 
    8.     mapStudent[3] = "student_three"; 
    9.  
    10. ********************************************************************/ 
    11. #pragma warning (disable:4786) 
    12. #include <map> 
    13. #include <string> 
    14. #include <iostream> 
    15.  
    16. using namespace std; 
    17.  
    18. int main() 
    19.     map<int, string> mapStudent; 
    20.     cout<<"三种插入方式:"<<endl; 
    21.  
    22.     mapStudent.insert(pair<int, string>(1, "student_one")); 
    23.     mapStudent.insert(map<int, string>::value_type(2, "student_two")); 
    24.     mapStudent[3] = "student_three"
    25.     mapStudent.insert(map<int, string>::value_type(4, "student_four"));    
    26.      
    27.     pair<map<int,string>::iterator,bool> InsertPair;   //判断是否插入成功 
    28.     InsertPair = mapStudent.insert(map<int,string>::value_type(5,"student_five")); 
    29.     if(InsertPair.second == true
    30.     { 
    31.         //cout<<InsertPair.first.operator++<<endl;  //求解??不知道怎么应用第一个数据 
    32.     } 
    33.  
    34.     cout<<"三种遍历方式:"<<endl; 
    35.  
    36.     map<int, string>::iterator  iter; 
    37.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 
    38.     { 
    39.         cout<<iter->first<<" "<<iter->second<<endl; 
    40.     } 
    41.  
    42.     map<int, string>::reverse_iterator  iters; 
    43.     for(iters = mapStudent.rbegin(); iters != mapStudent.rend(); iters++) 
    44.     { 
    45.         cout<<iters->first<<" "<<iters->second<<endl; 
    46.     }  //逆序输出 
    47.     cout<<"数组的输出形式:"<<endl; 
    48.     for(int iIndex=0;iIndex < mapStudent.size();iIndex++) //size()返回成员的个数 
    49.     { 
    50.         cout<<mapStudent[iIndex]<<endl; 
    51.     } 
    52.     cout<<mapStudent.count(1)<<endl;  //count()判断关键字是否存在,返回1表示存在,0 
    53.     iter = mapStudent.find(1);        //find()关键字存在时,返回数据所在位置的迭代器,否则返回end()返回的迭代器 
    54.     if( iter != mapStudent.end() ) 
    55.     { 
    56.         cout<<"数据存在:"<<iter->first<<" "<<iter->second<<endl; 
    57.         mapStudent.erase(iter);        //用迭代删除数据 
    58.     } 
    59.     else 
    60.     { 
    61.         cout<<"数据不存在!"<<endl; 
    62.     } 
    63.     int n = mapStudent.erase(3);        //用关键字删除,如果删除了会返回1,否则返回0 
    64.  
    65.     iter = mapStudent.lower_bound(2);   //返回2的迭代器 
    66.     cout<<iter->second<<endl; 
    67.     iter = mapStudent.upper_bound(2);   //返回3的迭代器 
    68.     cout<<iter->second<<endl;     
    69.  
    70.     /*Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字*/ 
    71.     pair<map<int,string>::iterator,map<int,string>::iterator> MapPair; 
    72.     MapPair = mapStudent.equal_range(2); 
    73.     if( MapPair.first == MapPair.second ) 
    74.     { 
    75.         cout<<"Do not find"<<endl; 
    76.     } 
    77.     else 
    78.     { 
    79.         cout<<"Find"<<endl; 
    80.     } 
    81.  
    82.     //删除一个前闭后开的集合,这是STL的特性 
    83.     mapStudent.earse(mapStudent.begin(), mapStudent.end());     
    84. }

  • 相关阅读:
    解决使用OCI连接oracle LNK2019: 无法解析的外部符号的问题
    VS2010下配置OCI编程
    OpenLayers简单介绍以及简单实例
    浏览器的标准模式与怪异模式的设置与区分方法
    解决ie7不支持after、before的方法
    ie7兼容after、before的方法
    【移动端适配】适配1个像素的border
    js实现对table的增加行和删除行的操作
    css3线性渐变:linear-gradient
    使用iScroll实现上、下滑动刷新和加载更多数据
  • 原文地址:https://www.cnblogs.com/hdk1993/p/5852677.html
Copyright © 2011-2022 走看看