UVa 156 <map>启蒙

map函数的启蒙篇,如何实现字符串对于数值的映射达到查重效果(map中同一个元素只能存在一次)。
map函数提供了find()以及count()。

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

vector<string>  word;
map <string, int> map1;

string fun(string x)
{
    string temp = x;
    for(int i = 0 ; i < (int)temp.length() ; i++)
        temp[i] = tolower(temp[i]);
    sort(temp.begin(), temp.end());
    return temp;
}

int main()
{
    string s;
    while(cin >> s)
    {
        if(s[0] == '#' )
            break;

        word.push_back(s);
        string low = fun(s);
        if(!map1.count(low))
            map1[low] = 0;
        map1[low]++;
    }

    vector<string> ans;
    for(int i = 0 ; i < (int)word.size(); i++)
        if(map1[fun ( word[i] ) ] == 1)
            ans.push_back(word[i]);

    sort(ans.begin(), ans.end());
    for(int i = 0 ; i < (int)ans.size(); i++)
        cout << ans[i] << endl;
    return 0;
}
透过泪水看到希望
原文地址:https://www.cnblogs.com/ronnielee/p/9502600.html