如何使用地图C ++删除重复项

所以我有Text.txt在哪里国家和河。所以我必须输出河和当前河存在的所有国家。

Text.txt:

Spanish Taho
France Rhine
Ukrain Dnipro
Germany Rhine
Russia Dnipro
Portugal Taho

码:

#include <iostream>
#include <map>
#include <string>
#include <fstream>

using namespace std;


int main()
{
    map<string, string> country;
    ifstream in("Text.txt");
    string key;
    string river;
    while (in >> key >> river)
    {
        country[key] = river;
    }
        map<string, string>::iterator it;
        map<string, string>::iterator it2;
        for (it = country.begin(); it != country.end(); it++) {
            cout << it->second << ":" << endl;
            for (it2 = country.begin(); it2 != country.end(); ++it2) {
                if (it->second == it2->second ) {
                    cout << "-" << it2->first << endl;
                }
            }
        }
    cout << endl;
    system("pause");
    return 0;
}

结果:

Rhine:
-France
-Germany
Rhine:
-France
-Germany
Taho:
-Portugal
-Spanish
Dnipro:
-Russia
-Ukrain
Taho:
-Portugal
-Spanish
Dnipro:
-Russia
-Ukrain

如何删除重复?

评论