C++ Map 基本使用

1.   map的构造函数: Map<int, string> mapStudent;

2.       数据的插入

#include <map>

#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;

//用insert函数插入pair数据
mapStudent.insert(pair<int, string>(1, "student_one"));   
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));

//用insert函数插入value_type数据
       //mapStudent.insert(map<int, string>::value_type(1, "student_one"));
//mapStudent.insert(map<int, string>::value_type(2, "student_two"));
//mapStudent.insert(map<int, string>::value_type(3, "student_three"));


      //用数组插入
       //mapStudent[1] =  "student_one";
//mapStudent[2] =  "student_two";
       // mapStudent[3] =  "student_three";


map<int, string>::iterator  iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

cout<<iter->first<<iter->second<<";";
}
return 0;
}

输出结果: 1student_one;2student_two;3student_three;

附:即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组可以。用数组的话先在mapStudent中查找主键为2的项,没发现,然后将一个新的对象插入mapStudent,键是2,值是一个空字符串,插入完成后,将字符串赋为"Two"; 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。

  a.  如何判断insert插入是否成功?

pair<map<int, string>::iterator, bool> Insert_Pair;
       Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
       if(Insert_Pair.second == true)
       {
           cout<<"Insert Successfully"<<endl;
       }
       else
       {
  cout<<"Insert Failure"<<endl;
       }
       Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
       if(Insert_Pair.second == true)
       {
  cout<<"Insert Successfully"<<endl;
       }
       else
       {
           cout<<"Insert Failure"<<endl;

       }

b. 测试数组插入是否可以

 mapStudent[1] =  "student_one";
mapStudent[2] =  "student_two";
    mapStudent[1] =  "student_three";


3. map的大小

int nSize = mapStudent.size();

4. 迭代

a. 应用前向迭代器:

map<int, string>::iterator  iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

cout<<iter->first<<iter->second<<";";
}

b. 应用反相迭代器:

map<int, string>::reverse_iterator  iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)

cout<<iter->first<<iter->second<<";";
}

c. 数组

int nSize = mapStudent.size();
for(int nIndex = 0; nIndex <= nSize; nIndex++)
{
cout<<mapStudent[nIndex]<<endl;
}

5. 查找

a. find

iter = mapStudent.find(1);
if(iter != mapStudent.end())
{
cout<<"Find, the value is "<<iter->second<<endl;
}
else
{
cout<<"Do not Find"<<endl;
}

b. lower_bound?

    upper_bound

6.  数据的删除

  1. iterator erase(iterator it); //通过一个条目对象删除
  2. iterator erase(iterator first, iterator last);        //删除一个范围
  3. size_type erase(const Key& key); //通过关键字删除

void Delete(map<int, string> mapStudent) 

{
map<int, string>::iterator iter;
iter = mapStudent.find(1);
mapStudent.erase(iter);


//如果要删除1,用关键字删除
int n = mapStudent.erase(1);//如果删除了会返回1,否则返回0 


//用迭代器,成片的删除
//一下代码把整个map清空
//成片删除要注意的是,也是STL的特性,删除区间是一个前闭后开的集合
mapStudent.erase(mapStudent.begin(), mapStudent.end());
}

 7.  数据的清空与判空

清空:clear()函数  相当于 enumMap.erase(enumMap.begin(), enumMap.end());

判空: empty()函数,返回true则说明是空

8. 排序

若关键字是int型,泽它本身支持小于号运算,在一些特殊情况,比如关键字是一个结构体,则有2个方法:

a. 在结构体里面重载比较函数

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;  //学生信息  

示例:

int nSize = 0;
//用学生信息映射分数
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;
}
cout<<"";

b.  在外面指定比较函数

class 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 nSize = 0;
//用学生信息映射分数
map<StudentInfo, int, sort>mapStudent;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值