compare two multimaps c++

后端 未结 3 1856
盖世英雄少女心
盖世英雄少女心 2020-12-10 23:13

I have two multimaps.i would like to create a new multimap which has the common key-value pairs in the given those two multimaps:

for eg:

#include &l         


        
3条回答
  •  佛祖请我去吃肉
    2020-12-10 23:22

    Use the std::set_intersection function template from

    std::multimap newMap;
    std::set_intersection(map1.begin(), map1.end(), 
                          map2.begin(), map2.end(), 
                          std::inserter(newMap, newMap.begin());
    

    Edit Yeah, apparently this doesn't work for a multimap as it would for a map. I suggest the following:

    std::multimap newMap;
    std::vector::value_type> v1(map1.begin(), map1.end());
    std::sort(v1.begin(), v1.end());
    std::vector::value_type> v2(map2.begin(), map2.end());
    std::sort(v2.begin(), v2.end());
    std::set_intersection(v1.begin(), v1.end(), 
                          v2.begin(), v2.end(), 
                          std::inserter(newMap, newMap.begin());
    

提交回复
热议问题