当前位置:网站首页 > 编程语言 > 正文

stlmap详解(stl map set)



map mapA;



•  pair< map ::iterator, bool > pairResult = mapA.insert(pair (3,"小张"));   //插入 方式



•  int iFirstFirst = (pairResult.first)->first;  //iFirst == 3;

•  string strFirstSecond = (pairResult.first)->second; //strFirstSecond为"小张"

•  bool bSecond = pairResult.second;   //bSecond == true;

•  

•  mapA.insert(map ::value_type(1,"小李"));  //插入方式二



•  mapA[3] = "小刘";   //修改value

•  mapA[5] = "小王";   //插入方式三



•  string str1 = mapA[2]; //执行插入 string() 操作,返回的str1的 字符串内容为空。

•  string str2 = mapA[3];   //取得value,str2为"小刘"


//迭代器遍历

•  for (map ::iterator it=mapA.begin(); it!=mapA.end(); ++it)

•  {

•   pair pr = *it;

•   int iKey = pr.first;

•   string strValue = pr.second;

•  }

map.rbegin()与map.rend()  略。






































































































































 map mapA;

•  mapA.insert(pair (3,"小张"));

•  mapA.insert(pair (1,"小杨"));

•  mapA.insert(pair (7,"小赵"));

•  mapA.insert(pair (5,"小王"));



•  map mapB(mapA);   //拷贝构造

•  

•  map mapC;

•  mapC = mapA;        //赋值



•  mapC[3] = "老张";

•  mapC.swap(mapA);   //交换







































































 map mapA;

•  mapA.insert(pair (3,"小张"));

•  mapA.insert(pair (1,"小杨"));

•  mapA.insert(pair (7,"小赵"));

•  mapA.insert(pair (5,"小王"));



•  if (mapA.empty())

•  {

•   int iSize = mapA.size();  //iSize == 4

•  }





















































map mapA;

•  mapA.insert(pair (3,"小张"));

•  mapA.insert(pair (1,"小杨"));

•  mapA.insert(pair (7,"小赵"));

•  mapA.insert(pair (5,"小王"));


•  // 删除区间内的元素

•  map ::iterator itBegin=mapA.begin();

•  ++ itBegin;

•  ++ itBegin;

•  map ::iterator itEnd=mapA.end();

•  mapA.erase(itBegin,itEnd);   //此时容器mapA包含按顺序的{1,"小杨"}{3,"小张"}两个元素。



•  mapA.insert(pair (7,"小赵"));

•  mapA.insert(pair (5,"小王"));



•  //删除容器中第 一个元素

•  mapA.erase(mapA.begin());  //此时容器mapA包含了按顺序的{3,"小张"}{5,"小王"}{7,"小赵"}三个元素

  //删除容器中key为5的元素

•  mapA.erase(5);  

  //删除mapA的所有元素

•  mapA.clear();   //容器为空


























































































































map mapA;

•  mapA.insert(pair (3,"小张"));

•  mapA.insert(pair (1,"小杨"));

•  mapA.insert(pair (7,"小赵"));

•  mapA.insert(pair (5,"小王"));



•  //find方法

•  map ::iterator it1 = mapA.find(5);  //it1指向{5,"小王"}元素



•  int iCount = mapA.count(5);   //iCount == 1



•  //lower_bound方法

•  map ::iterator it2 = mapA.lower_bound(5);  //it2指向{5,"小王"}元素



•  //upper_bound方法

•  map ::iterator it3 = mapA.upper_bound(5);  //it3指向{7,"小赵"}元素



•  //equal_range方法

•  pair< map ::iterator, map ::iterator > pr = mapA.equal_range(5);



•  map ::iterator it4 = pr.first;   //it4指向{5,"小王"}元素

•  map ::iterator it5 = pr.second;  //it5指向{7,"小赵"}元素

































































































































到此这篇stlmap详解(stl map set)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • 怎么看单播地址(单播地址是什么意思)2026-03-20 17:54:05
  • git 指定版本(git指定版本合并)2026-03-20 17:54:05
  • 认证码(谷歌怎么收到认证码)2026-03-20 17:54:05
  • 速排蚂蚁编辑器怎么上传视频(蚂蚁编辑器怎么用)2026-03-20 17:54:05
  • 女神异闻录5战斗系统任务(女神异闻录5r战斗)2026-03-20 17:54:05
  • 动态库和静态库谁快(静态库和动态库的概念)2026-03-20 17:54:05
  • mhaal00是华为手机的什么型号(mhaal00是华为什么型号多少钱)2026-03-20 17:54:05
  • 操作系统原理课程(操作系统原理网课)2026-03-20 17:54:05
  • stat工具(st工具箱)2026-03-20 17:54:05
  • 接口400是什么错误类型(接口403错误怎么解决)2026-03-20 17:54:05
  • 全屏图片