题目笔记 UVA156

STL map

UVA156  AC码

#include<iostream>
#include<vector>
#include<cctype>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;

string s;
map<string,int> we;
vector <string> words; 
vector <string> ans;

string standarded (const string& a)//标准化
{
    string ans=a;
    for(int i=0;i<ans.length();i++) ans[i]=tolower(ans[i]);
    sort(ans.begin(),ans.end());
    return ans;
}

int main()
{
    while(cin>>s)
    {
        if(s[0]=='#') break;
        words.push_back(s);
        string stan=standarded(s);
    }

    for(int i=0;i<words.size();i++)
    {
        if(we[standarded(words[i])]==1) ans.push_back(words[i]); //找到合适的单词(唯一存在)
    }
    sort(ans.begin(),ans.end());//字典序
    for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl;
    return 0;
}

tips

map初始化

map<string,int> we

//本题
    if(!we.count(stan)) 
        we[stan]=0;      //如果在we这个map中Stan的数量==0,赋给stan权值为0
    we[stan]++;         


//常规
    we["hi"]=1;    

sort字典序

sort(ans.begin(),ans.end());         //传入两个迭代器(类似于数组传入地址)

又是引用

我还不太会一会去看个视频学一下

string standarded (const string& a)//标准化
{
    string ans=a;
    for(int i=0;i<ans.length();i++) ans[i]=tolower(ans[i]);
    sort(ans.begin(),ans.end());
    return ans;
}
原文地址:https://www.cnblogs.com/juuich/p/12388914.html