c++ map的使用

/*

7、map的基本操作函数:

C++ Maps是一种关联式容器,包含“关键字/值”对

begin() 返回指向map头部的迭代器

clear() 删除所有元素

count() 返回指定元素出现的次数

empty() 如果map为空则返回true

end() 返回指向map末尾的迭代器

equal_range() 返回特殊条目的迭代器对

erase() 删除一个元素

find() 查找一个元素

get_allocator() 返回map的配置器

insert() 插入元素

key_comp() 返回比较元素key的函数

lower_bound() 返回键值>=给定元素的第一个位置

max_size() 返回可以容纳的最大元素个数

rbegin() 返回一个指向map尾部的逆向迭代器

rend() 返回一个指向map头部的逆向迭代器

size() 返回map中元素的个数

swap() 交换两个map

upper_bound() 返回键值>给定元素的第一个位置

value_comp() 返回比较元素value的函数

cctype 头文件的一些方法

isalnum(c) 假如c是字母或数字,则为true

isalpah(c) 假如c是字母,则为true

iscntrl(c) 假如c是控制字符,则为true

isdigit(c) 假如c是数字,则为true

isgraph(c) 假如c不是空格,则为true

islower(c) 假如c是小写字母,则为true

isprint(c) 假如c是可打印的字符,则为true

ispunct(c) 假如c是标点符号,则为true

isspace(c) 假如c是空白字符,则为true

isupper(c) 假如c是大写字母,则为true

isxdigit(c) 假如c是十六进制数,则为true

tolower(c) 假如c是大写字母,则返回小写字母形式,否则返回c。

toupper(c) 假如c是小写字母,则返回大些字母形式,否则返回c。

问题:输入一些单词,找出满足所有要求如下的单词:该单词不能通过字母重排,得到输入文本中的另一个单词.

在判断是否满足条件时,字母不分大小写,但是输出时应保留输入中的大小写,按字典序进行排序(所有大写字母在小写字母的前面)

*/

#include<iostream>

#include<string>

#include<vector>

#include<map>

#include<algorithm>

#include<cctype>

using namespace std;

map<string,int> cnt;

vector<string> words;

//将单词进行"标准化"

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);//保存到vector中

string r=repr(s);

//cnt map中是否包含r

if(!cnt.count(r))cnt[r]=0;

cnt[r]++;//赋值为1

}

vector<string> ans;

//遍历words

for(int i=0;i<words.size();i++)

if(cnt[repr(words[i])]==1)ans.push_back(words[i]);//找出int值为1的cnt元素 重新赋值到新的vertor中

sort(ans.begin(),ans.end());//排序

for(int i=0;i<ans.size();i++)//遍历输出

cout<<ans[i]<<endl;

return 0;

}