9.29 集合set与映射map 之map

一、今日内容

map

1·重载了[ ],像数组高级版

2·写法:例:map<string,int>  month_name表示月份名到月份编号;赋值: month_name["July"] = 7

3·例程:Ananagrams

#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
map<string,int> cnt;
//将单词s进行标准化,小写
string repr(const string& s)
{
string ans = s;
for(int i = 0;i < ans.length(); i++)
ans[i] = tolower(ans[i]);
sort(ans.begin(),ans.end());
return ans;
}


int main()
{
int n = 0;
string s;
while(cin >> s)
{
if(s[0] == '#') break;
words.push_back(s);
string r = repr(s);
if(!cnt.count(r)) cnt(r) = 0;
cnt[r]++;
}
vector<string> ans;
for(int i = 0; i < words.size(); i++)
if(cnt[repr(words[i])] == 1) ans.push_back(words[i]);
sort(ans.begin(),ans.end());
for(int i = 0; i < ans.size();i++)
cout<<ans[i]<<'\n';
return 0;
}

此题与Andy's First Dictionary 的区别 在于 A用set,此题用map;A仅按字典顺序排序即可,不区分大小写;此题要求较多。

总之,没看懂。

二、明天计划:a·结合c++标准程序库再研究研究算法的一些基础知识,不能急,基础先打牢;

b·结合c++教材 回顾模板部分。这玩意得慢慢啃,静下来慢慢干。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值