c++ - compare two maps and identify distinct elements -
compare 2 maps (std::map<std::string, std::unsigned int>
) value , identify distinct elements.
typedef std::map<std::string /* file name*/, unsigned int /*crc*/> mmap;
list of files each file in list calculated crc. file name not changed, crc changet.
if have 2 maps, distinct entries, named oldmap
, newmap
, can use std::set_difference
unique entries both maps. see following example of how can done.
#include <map> #include <string> #include <algorithm> #include <utility> #include <iterator> #include <iostream> int main() { // 2 maps containing values. std::map<std::string, int> oldmap { {"test1", 1}, {"test2", 2}, {"test3", 3} }; std::map<std::string, int> newmap { {"test2", 2} }; // create new map holds distinct pairs. std::map<std::string, int> diffmap; // add distinct pairs diffmap. std::set_difference(begin(oldmap), end(oldmap), begin(newmap), end(newmap), std::inserter(diffmap, begin(diffmap))); // output result (auto& p : diffmap) { std::cout << p.first << " " << p.second << std::endl; } }
output:
test1.txt 1 test3.txt 3
these 2 entries not found in both maps.
Comments
Post a Comment