博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ map multimap操作
阅读量:5244 次
发布时间:2019-06-14

本文共 3759 字,大约阅读时间需要 12 分钟。

#include <iostream>

#include <map>
#include <string>

using namespace std;

int main()

{
  map<int, string> map1;
  multimap<int, string> multimap1;

  map1.insert(map<int,string>::value_type(1,"One"));

  map1.insert(map<int, string>::value_type(2, "Two"));
  map1.insert(map<int, string>::value_type(3, "Three"));
  map1.insert(make_pair(-1,"Minus One"));
  map1.insert(pair<int,string>(1000,"Four"));
  map1[100] = "Five";

  cout << "map1 size:" << map1.size() << endl;

  map<int, string>::const_iterator map_iter;
  for (map_iter =map1.begin();map_iter !=map1.end();++map_iter)
  {
    cout << "key:" << map_iter->first << " value:" << map_iter->second.c_str() << endl;
  }

  cout << "---------------------------"<< endl;

  multimap1.insert(multimap<int,string>::value_type(1,"One"));

  multimap1.insert(multimap<int, string>::value_type(1, "One"));
  multimap1.insert(multimap<int, string>::value_type(2, "Two"));
  multimap1.insert(pair<int,string>(2,"Test"));
  multimap1.insert(pair<int,string>(2,"Test2"));
  multimap1.insert(make_pair<int,string>(3,"Test"));

  cout << "multimap size:" << multimap1.size() << endl;

  multimap<int, string>::const_iterator multimap_iter;
  for (multimap_iter = multimap1.begin();multimap_iter != multimap1.end();++multimap_iter)
  {
    cout << "key:" << multimap_iter->first << " value:" << multimap_iter->second.c_str() << endl;
  }

  cout << "element count:" << multimap1.count(1) << endl;

  cout << "-----------------------------------------" << endl;

  multimap<int, string>::const_iterator iter2;
  iter2 = multimap1.find(2);
  if (iter2 != multimap1.end())
  {
    size_t element_count = multimap1.count(2);
    for (int k=0;k<element_count;k++)
    {
      cout << "find: "<<k<<" " << iter2->first << " " << iter2->second.c_str() << endl;
      ++iter2;
    }
    
  }
  else
  {
    cout << "not found..." << endl;
  }

  cout << "---------------------------------------------"<<endl;

  if (multimap1.erase(2)>0)
  {
    cout << "delete element success..."<<endl;
  }
  multimap<int, string>::const_iterator multimap_iter1;
  for (multimap_iter1 = multimap1.begin(); multimap_iter1 != multimap1.end(); ++multimap_iter1)
  {
    cout << "key:" << multimap_iter1->first << " value:" << multimap_iter1->second.c_str() << endl;
  }
  cout << "---------------------------------------------" << endl;
  multimap<int, string>::iterator iter3 = multimap1.find(1);
  if (iter3 != multimap1.end())
  {
    size_t element_count = multimap1.count(1);
    for (int k=0;k<element_count;k++)
    {
      iter3 = multimap1.find(1);
      multimap1.erase(iter3);
      //++iter3;
      cout << "delete success..." << endl;
    }
  }
  cout << "---------------------------------------------" << endl;
  multimap<int, string>::const_iterator multimap_iter2;
  for (multimap_iter2 = multimap1.begin(); multimap_iter2 != multimap1.end(); ++multimap_iter2)
  {
    cout << "key:" << multimap_iter2->first << " value:" << multimap_iter2->second.c_str() << endl;
  }

  system("pause");

  return 0;
}

 

====================================== 

 

map1 size:6

key:-1 value:Minus One
key:1 value:One
key:2 value:Two
key:3 value:Three
key:100 value:Five
key:1000 value:Four
---------------------------
multimap size:6
key:1 value:One
key:1 value:One
key:2 value:Two
key:2 value:Test
key:2 value:Test2
key:3 value:Test
element count:2
-----------------------------------------
find: 0 2 Two
find: 1 2 Test
find: 2 2 Test2
---------------------------------------------
delete element success...
key:1 value:One
key:1 value:One
key:3 value:Test
---------------------------------------------
delete success...
delete success...
---------------------------------------------
key:3 value:Test
请按任意键继续. . .

 

转载于:https://www.cnblogs.com/herd/p/10996273.html

你可能感兴趣的文章
无刷新分页代码,jQuery分页完整示例
查看>>
鼠标悬停,会变亮的CSS按钮
查看>>
云计算之路-阿里云-分享:通过RDS备份文件恢复SQL Server数据库
查看>>
AutoHotKey实现软件授权
查看>>
MySQL 多表查询
查看>>
【Kafka源码】日志处理
查看>>
USACO Section1.1 Friday the Thirteenth 解题报告
查看>>
HDU5908 Abelian Period 暴力
查看>>
Spring Security教程(三)
查看>>
第七周作业
查看>>
iOS开发数据库篇—SQLite简单介绍
查看>>
斜率优化DP
查看>>
windows在python基础上安装pip
查看>>
Street Number (UVA 138)
查看>>
尝试读取或写入受保护的内存。这通常指示其他内存已损坏
查看>>
tensorflow学习
查看>>
51nod1201 整数划分
查看>>
DES加密算法应用:分组加密模式
查看>>
[django]celery_redis探索
查看>>
Appium python自动化测试系列之使用HTMLTestRunner生成测试报告(十三)
查看>>