zoukankan      html  css  js  c++  java
  • C++ map insert 另一个map的子集

    C++map中 会有insert操作,举个例子

    存在map A,我们截取一部分到map B中,void insert (InputIterator first, InputIterator last) ,截取的部分是 first 到 last 前一个迭代器的值

    // map::insert (C++98)
    #include <iostream>
    #include <map>
    
    int main ()
    {
      std::map<char,int> mymap;
    
      // first insert function version (single parameter):
      mymap.insert ( std::pair<char,int>('a',100) );
      mymap.insert ( std::pair<char,int>('z',200) );
    
      std::pair<std::map<char,int>::iterator,bool> ret;
      ret = mymap.insert ( std::pair<char,int>('z',500) );
      if (ret.second==false) {
        std::cout << "element 'z' already existed";
        std::cout << " with a value of " << ret.first->second << '
    ';
      }
    
      // second insert function version (with hint position):
      std::map<char,int>::iterator it = mymap.begin();
      mymap.insert (it, std::pair<char,int>('b',300));  // max efficiency inserting
      mymap.insert (it, std::pair<char,int>('c',400));  // no max efficiency inserting
    
      // third insert function version (range insertion):
      std::map<char,int> anothermap;
      anothermap.insert(mymap.begin(),mymap.find('c'));
    
      // showing contents:
      std::cout << "mymap contains:
    ";
      for (it=mymap.begin(); it!=mymap.end(); ++it)
        std::cout << it->first << " => " << it->second << '
    ';
    
      std::cout << "anothermap contains:
    ";
      for (it=anothermap.begin(); it!=anothermap.end(); ++it)
        std::cout << it->first << " => " << it->second << '
    ';
    
      return 0;
    }

    输出结果:

    element 'z' already existed with a value of 200
    mymap contains:
    a => 100
    b => 300
    c => 400
    z => 200
    anothermap contains:
    a => 100
    b => 300

    详细信息可参考:  http://www.cplusplus.com/reference/map/map/insert/

  • 相关阅读:
    puppet master/agent
    puppet单机模型
    Nginx MogileFS 配置
    mogilefs 安装与配置
    CMakeLists.txt
    下载安装MariaDB Galera 10.1
    BZOJ1295: [SCOI2009]最长距离
    BZOJ2375: 疯狂的涂色
    BZOJ1260: [CQOI2007]涂色paint
    BZOJ2789: [Poi2012]Letters
  • 原文地址:https://www.cnblogs.com/r-yan/p/11673108.html
Copyright © 2011-2022 走看看