我已经声明了两个 map m1
和 m2
。
Map m1 的键在m2 的键中。但是m2的所有key都不在m1的key中。
谁能帮我找出 m2 中与 m1 中的键相比不常见的键?
例子
m1 包含:
3=> 1 2 4
6=> 3 4 6
m2 包含:
3 => 3 5 6
6 => 6 4 8
8 => 2 4 3
10 => 2 5 7 9
输出将是 8 和 10。
最佳答案
你可以通过 std::set_difference 来完成.示例:
std::map<int, std::string> m1;
m1[3] = "1 2 4";
m1[6] = "3 4 6";
std::map<int, std::string> m2;
m2[3] = "3 5 6";
m2[6] = "6 4 8";
m2[8] = "2 4 3";
m2[10] = "2 5 7 9";
std::map<int, std::string> m3;
std::set_difference(m2.begin(), m2.end(), m1.begin(), m1.end(), std::inserter(m3, m3.begin()), m1.value_comp());
for (auto i = m3.begin(); i != m3.end(); ++i) {
std::cout << "[" << i->first << "," << i->second << "]";
}
std::cout << std::endl;
结果:
[8,2 4 3][10,2 5 7 9]
关于c++ - 如何找到不在另一个 map 的键中的 map 键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26376033/