检查map <string,string>是否包含另一个map <string,string>

use*_*755 2 c++ dictionary stl

我在c ++中有一个字符串映射,并想检查第一个映射是否包含在另一个映射中.例如

map<string, string> mA = {{"a", "a1"}, {"b", "b1"}, {"c", "c1"}};
map<string, string> mB = {{"b", "b1"}, {"a", "a1"}};

bool contained = isContained(mB, mA);

// isContained returns true iff every key value pair from mB is contained in mA.
// in this case is true because the pair <"b", "b1"> is contained in mA,
// and the pair <"a", "a1"> is contained too.
Run Code Online (Sandbox Code Playgroud)

我更喜欢使用STL中的一些函数来使我的代码更清晰.

请注意,地图中没有特定的排序.

例如,在java中,这可以很容易地使用

h2.entrySet().containsAll(h1.entrySet())
Run Code Online (Sandbox Code Playgroud)

但说实话,我不知道如何用c ++解决它.

Ben*_*ley 5

std::includes(mA.begin(), mA.end(),
              mB.begin(), mB.end());
Run Code Online (Sandbox Code Playgroud)

这仅适用于已排序的容器,即std::map.但是unordered_map,例如,它不会起作用.另请注意,这会将映射值考虑在内.为了忽略它,只比较键,您可以传递自定义比较std::includes.