2013-07-21 2 views
-1

은 값으로 두 개의 맵 (std::map<std::string, std::unsigned int>)을 비교하고 고유 한 요소를 식별합니다.은 두 개의 맵을 비교하고 고유 한 요소를 식별합니다.

typedef std::map<std::string /* file name*/, unsigned int /*crc*/> mMap; 

목록의 각 파일에 대한 파일 목록은 crc로 계산됩니다. 파일 이름은 변경되지 않습니다. crc changet.

+2

무엇이 문제입니까? – Borgleader

+0

http://www.cplusplus.com/reference/algorithm/set_difference/을보십시오. 또한 이것은 매우 잘못 구성된 질문입니다. – Chad

답변

0

파일 이름이 중요하고 CRC가 값이고 새 파일 및/또는 crc 변경 사항을 확인하려는 2 개의 맵이 있다고 생각합니다.

왜 이렇게하지 않습니까? 당신이 oldMapnewMap라는 이름의 일부 별개의 항목, 두 개의지도가있는 경우

set<string> common; 
for (auto it = mapOld.begin(); it != mapEnd.begin(); ++it) 
    if (mapNew.find(it->second) == mapNew.end()) 
     cout << "file deleted " << it->second << endl; 
    else 
     common.insert(it->second); 
for (auto it = mapNew.begin(); it != mapEnd.begin(); ++it) 
    if (mapOld.find(it->second) == mapOld.end()) 
     cout << "file added " << it->second << endl; 
    else 
     common.insert(it->second); 
for (auto it = common.begin(); it != common.end(); ++it) 
    if (mapOld[*it] != mapNew[*it]) 
     cout << "CRC changed for " << *it << endl; 
1

, 당신은 둘지도에서 고유 항목을 얻을 수 std::set_difference를 사용할 수 있습니다. 다음 예제를 참조하십시오.

#include <map> 
#include <string> 
#include <algorithm> 
#include <utility> 
#include <iterator> 
#include <iostream> 

int main() { 
    // Two maps containing some values. 
    std::map<std::string, int> oldMap { {"test1", 1}, {"test2", 2}, {"test3", 3} }; 
    std::map<std::string, int> newMap { {"test2", 2} }; 

    // Create new map that holds distinct pairs. 
    std::map<std::string, int> diffMap; 

    // Add distinct pairs to diffMap. 
    std::set_difference(begin(oldMap), end(oldMap), 
         begin(newMap), end(newMap), 
         std::inserter(diffMap, begin(diffMap))); 

    // Output result 
    for (auto& p : diffMap) { 
     std::cout << p.first << " " << p.second << std::endl; 
    } 
} 

출력 :

test1.txt 1 
test3.txt 3 

이 모두지도에없는 두 개의 항목이 있습니다.

관련 문제