• c++ map multimap操作


    #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
    请按任意键继续. . .

  • 相关阅读:
    你们要的Intellij IDEA 插件开发秘籍,来了!
    【JDK 11】关于 Java 模块系统,看这一篇就够了
    《水浒传》中的物价
    读书廿一日计划
    诗词记录
    PLSQL14不识别Oracle数据库以及tnsnames.ora中配置的连接串(连接远程Oracle,本地仅安装客户端)
    框架安全之Shiro渗透
    .NET 3.5 安装
    中间件安全之Nginx渗透
    中间件安全之JBoss渗透
  • 原文地址:https://www.cnblogs.com/herd/p/10996273.html
Copyright © 2020-2023  润新知