用insert重写单词计数程序
来源:互联网 发布:mac轻淘客插件 编辑:程序博客网 时间:2024/05/16 05:03
int main (int argc, char* argv[])
{
std::ios::sync_with_stdio (false);
//忽略列表
setexclude ={ "fuck", "damn" };
stringword;
map<string, size_t> word_count;
while (cin>> word) {
//大小写不敏感, 全部转换为小写
for (auto &ch : word)
ch = tolower (ch);
//只输入忽略列表中不在的单词
if (exclude.find (word) == exclude.end ()){
//ret的类型是pair< map< string, size_t>::iterator, bool>
auto ret = word_count.insert (make_pair(word,1));
if (!ret.second)
//ret.first是一个word_count的迭代器
//ret.first->解引用
//++ret.first->second迭代器指向的计数器++
++ret.first->second;
}
}
for (auto it= word_count.cbegin (); it != word_count.cend (); ++it) {
cout << "The " << it-> first<< " has appered ";
cout << it->second << ((it->second > 1) ? " times" : " time") << endl;
}
return0;
}
或者直接:
while (cin >> word)
++word_count.insert ({ word, 0 }).first->second;
C++博大精深,由此可见一斑.
{
}
或者直接:
while (cin >> word)
C++博大精深,
0 0
- 用insert重写单词计数程序
- 用insert重写单词计数程序
- 单词计数程序
- 《C++ Primer》P314中使用insert重写单词统计程序的扩展
- 也写单词计数程序
- C语言二叉排序树单词计数程序实现
- 带有状态的SparkStreaming单词计数程序
- Storm集群部署与单词计数程序
- 用Scala进行单词计数
- 单词计数
- 单词计数
- 单词计数
- 单词计数
- 单词计数
- 单词计数
- 单词计数
- 单词计数
- 499.Insert Interval-单词计数 (Map Reduce版本)(容易题)
- 读取文件, 统计字符,忽略大小写
- 读入一个string序列和一个int序列,…
- 将输入的string中"yes" 改为 "no"
- 不用加减乘除做加法
- multiset<string>与vector<string>的比较
- 用insert重写单词计数程序
- 输入孩子的姓名, 用map存储,&…
- struts2执行原理简易代码
- Flex 接口定义
- ios search bar的bug
- 如何美化你的Eclipse
- 用commons-fileupload-1.2 实现文件上传
- 深度搜索C++对象模型2.2 构造函数语义学-Default Constructor的构造操作
- 数字矩形