首页 > 其他 > 详细

关联容器的操作(pair,map)

时间:2020-07-21 12:14:29      阅读:57      评论:0      收藏:0      [点我收藏+]
示例代码:
#include <map>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
  string name="jack";
  pair<int,string> pair1(1,name);
  pair<int,string> pair2(2,"老张");
  map<int,string> map1;
  map1.insert(pair1);
  map1.insert(pair2);
  cout<<"通过find函数返回的迭代器访问键值为1的关联条目"<<endl;
  auto it = map1.find(1);
  if(it!=map1.end())
  {
    string name = it ->second;
    std::cout << name << endl;
  }
  std::cout << "访问键值为2的关联条目" << endl;
  std::cout << "名字" << map1[2]<<endl;
  std::cout << "删除键值为2的pair" << endl;
  map1.erase(2);
  std::cout << "访问键值为2的关联条目" << endl;
  std::cout << "名字:" << map1[2] << endl;
  return 0;
}
结果:
技术分享图片

 

 

关联容器的操作(pair,map)

原文:https://www.cnblogs.com/shiheyuanfang/p/13352699.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!