Find if one map is subset of another

匿名 (未验证) 提交于 2019-12-03 00:48:01

问题:

I have two STL maps std::map<int, int> foo = {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}}; and std::map<int, int> bar = {{2, 0}, {4, 0}, {5, 0}};

I want to find if bar is a subset of foo.

Since the elements are sorted in map, I would think to find the first element from bar in foo, and then find consecutive elements from bar in foo from that location.

The problem here is I'm not able to figure out a way to do that with STL maps in cpp. Can I reduce the search range in map for every find from a location in map to the end of the map?

I hope I explained the problem.

回答1:

Use std::includes algorithm with a custom comparator that compares only the keys:

#include <map> #include <algorithm> #include <iostream>  int main() {     std::map<int, int> foo = {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}};     std::map<int, int> bar = {{2, 0}, {4, 0}, {5, 0}};     typedef std::pair<int,int> pair;      std::cout <<        std::includes(foo.begin(), foo.end(), bar.begin(), bar.end(),            [](const pair& p1, const pair& p2)            {                return p1.first < p2.first;            }); } 


回答2:

You could extract key sets (set1 and set2) of both maps (foo and bar), and as long as they are sorted, you can do the following:

if (std::includes(set1.begin(), set1.end(),                   set2.begin(), set2.end())) {   // ... } 

See std::includes.



回答3:

A simple way is to use Boost.Range in combination with boost::includes:

using namespace boost::adaptors; bool result = includes(foo | map_keys, bar | map_keys); 

Here is how a minimal, complete program could look like (mapped values are disregarded):

#include <map> #include <iostream> #include <boost/range.hpp> #include <boost/range/adaptors.hpp> #include <boost/range/algorithm.hpp>  int main() {     std::map<int, int> foo = {{1, 0}, {2, 0}, {3, 0}, {4, 0}, {5, 0}, {6, 0}};     std::map<int, int> bar = {{2, 0}, {4, 0}, {5, 0}};      using namespace boost::adaptors;     std::cout << includes(foo | map_keys, bar | map_keys); } 

Here is a live example.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!