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/

  • 相关阅读:
    spring整合freemarker 自定义标签
    curl 取不到第二个参数解决方法
    solr5.5教程-solr.home 配置
    solr5.5教程-schema.xml部分配置
    solr5.5教程-solrconfig.xml,加载schema.xml
    solr5.5教程-tomcat布署(2)
    solr5.5教程-tomcat布署
    jsp页面el表达式不起作用
    spring+hibernate--直接修改数据库,再通过hibernate查询数据不变
    13 hbase连接
  • 原文地址:https://www.cnblogs.com/r-yan/p/11673108.html
Copyright © 2011-2022 走看看