[C++]STL中map的用法


大家好,有时候读代码,写代码用到了,讲下map 。首先说下stl_map.h 中class map 很长。

原理:红黑树,对数据自动排序,可以做到一一数据映射关系。

map的构造函数:

map 作为STL中一个关联容器,提供一对一的数据处理能力,

1、有6个构造函数 比如

Map<int, string> mapStudent

2、数据的插入insert

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::iterator  iter;
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
       Cout<<iter->first<<”   ”<<iter->second<<end;
}
}
3、map的大小 size()

map<int, string> mapStudent;
int nSize = mapStudent.size();
4、数据的遍历

(1)第一个就是上面的前向迭代器。

(2)反向迭代器

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
       for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
       Cout<<iter->first<<”   ”<<iter->second<<end;
}
}
(3) 数组

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
      int nSize = mapStudent.size();
     for (int nIndex = 1; nIndex <= nSize; nIndex++)
 // for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
    {
       //Cout<<iter->first<<”   ”<<iter->second<<end;
      cout << mapStudent[nIndex] << endl;
 }
}
5、数据查找 find

我们通过find函数 来定位数据出现的位置

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
      iter = mapStudent.find(1);
     if(iter! = mapStudent.end())
    {
        cout << "Find the value is" << iter->second <<endl;
     }
     else
    {
        cout << "do not find"  <<endl;

6、数据的清空clear()与判空empty(),返回ture 这为空map

7、数据的删除 erase()

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
      iter = mapStudent.find(1);
      mapStudent.erase(iter);          // (1)迭代器删除
     int n = mapStudent.erase(1);  // (2)关键字删除
    //(3)用迭代器,成片的删除
     mapStudent.erase(mapStudent.begin(),mapStudent.end());
 

for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
       Cout<<iter->first<<”   ”<<iter->second<<end;
}

8、swap key_comp,value_comp,get_allocator

9、排序(特殊的情况,关键字为一个结构体,排序问题)

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

10、结尾
map的每个数据对应红黑树上的一个节点,这个节点在不保存你的数据时,是占用16个字节的,一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),这些比较占内存。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值