STL-map容器

Map用法详解

1.    map的构造函数

Map<int, string> mapStudent;

2.    数据的插入

三种插入数据的方法:

第一种:用insert函数插入pair数据

#pragmawarning (disable:4786) )
Map<int, string>mapStudent;
mapStudent.insert(pair<int,string>(1, “student_one”));
map<int, string>::iterator iter;
 for(iter =mapStudent.begin(); iter != mapStudent.end(); iter++)
{
    Cout<<iter->first<<” ”<<iter->second<<end;
}


第二种:用insert函数插入value_type数据

Map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1, “student_one”));
map<int, string>::iterator iter;
  for(iter = mapStudent.begin(); iter != mapStudent.end();iter++) 
{
    Cout<<iter->first<<” ”<<iter->second<<end;
}


第三种:用数组方式插入数据

Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
map<int, string>::iterator iter; 
  for(iter = mapStudent.begin(); iter != mapStudent.end();iter++)
{
    Cout<<iter->first<<” ”<<iter->second<<end;
}


3.    map的大小

Int nSize = mapStudent.size();

4.    数据的遍历

5.数据的查找(包括判定这个关键字是否在map中出现)

第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了

第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器

 

map<int, string>::iterator iter;
 iter = mapStudent.find(1);
 
if(iter != mapStudent.end())
{
    Cout<<”Find, the value is”<<iter->second<<endl;
}

6.数据的清空与判空

清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map

7.数据的删除

这里要用到erase函数,它有三个重载了的函数
  

  //如果要删除1,用迭代器删除
    map<int, 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的特性,删除区间是一个前闭后开的集合

 

9. 排序

STL中默认是采用小于号来排序的,以上代码在排序上是不存在任何问题的,因为上面的关键字是int型,它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,涉及到排序就会出现问题,因为它没有小于号操作,insert等函数在编译的时候过不去,下面给出两个方法解决这个问题

 1.重载小于号

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));
 
}


 

原文地址:https://www.cnblogs.com/bryce1010/p/9387450.html