两张地图不匹配

Mismatch between two maps

我可以在两张地图上使用 std::mismatch 吗?

documentation 中有一个使用字符串的示例,我认为它与矢量类似。

Intersection of two STL maps的答案很有用,但我不知道如何在地图上使用std::mismatch

如果可以的话,它也可以用于嵌套地图吗?

是的,std::map 具有双向迭代器,而 std::mismatch 需要输入迭代器作为其参数:

std::map<int, int> A {{1, 2}, {2, 3}, {4, 4}};
std::map<int, int> B {{1, 2}, {2, 4}, {4, 4}};
auto miss = std::mismatch(A.begin(), A.end(), B.begin());
std::cout << "{" << miss.first->first << ", " << miss.first->second 
          << "} != {" << miss.second->first << ", " 
          << miss.second->second << "}" << std::endl;

输出:

{2, 3} != {2, 4}

LIVE DEMO