检查map<string,string>是否包含另一个map<string,string>
Check if map<string, string> contains another map<string, string>
我有一个 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.
我更喜欢使用 STL 中的一些函数,以使我的代码更简洁。
请注意,地图中没有特定的排序。
例如,在java中,这可以使用以下方法轻松解决
h2.entrySet().containsAll(h1.entrySet())
但老实说,我不知道如何在 c++ 中解决它。
std::includes(mA.begin(), mA.end(),
mB.begin(), mB.end());
这仅适用于已分类的容器,std::map
是这样。但它不适用于unordered_map
,例如。另请注意,这确实考虑了映射的值。为了忽略它,只比较键,您可以将自定义比较传递给std::includes
。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- EASTL矢量<向量<int>>连续的
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量