主观题:词频统计 - C/C++ 容器与模板*
使用map容器重构19.1节所示的WordsCount程序,并按照每个词的出现次数降序输出统计结果。<br>
19.1节程序源代码:
//Project - WordsCount
#include <iostream>
#include <vector>
#include <fstream>
#include <assert.h>
using namespace std;
int main() {
ifstream in("C:/C2Cpp/C19_Container/WordsCount/title.txt");
vector<string> words; //words[i]存放发现的第i个单词
vector<int> counts; //counts[i]存放words[i]在诗中的出现次数
string sWord;
while (in >> sWord){ //operator>>(in,sWord)
for (size_t i=0;i<words.size();i++){
if (words[i]==sWord){
counts[i]++;
sWord = "";
break;
}
}
if (sWord!=""){
words.push_back(sWord);
counts.push_back(1);
}
}
assert(words.size()==counts.size());//断言向量words及counts长度相等
for (size_t i=0;i<words.size();i++){
cout << words[i] << ":" << counts[i] << ", ";
}
return 0;
}
title.txt文内文件内容:
When you are old
--- William Butler Yeats
When you are old and grey and full of sleep,
And nodding by the fire, take down this book,
And slowly read, and dream of the soft look
Your eyes had once, and of their shadows deep;
How many loved your moments of glad grace,
And loved your beauty with love false or true,
But one man loved the pilgrim soul in you,
And loved the sorrows of your changing face;
And bending down beside the glowing bars,
Murmur, a little sadly, how Love fled
And paced upon the mountains overhead
And hid his face amid a crowd of stars.
请提交一个PDF文件:
1. 包含你的学号及姓名;
2. 你的程序源代码;
3. 运行结果截图。
不接受PDF以外的文件格式。答题可在Word中编辑,然后再另存为PDF。
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)

答案:
19.1节程序源代码:
//Project - WordsCount
#include <iostream>
#include <vector>
#include <fstream>
#include <assert.h>
using namespace std;
int main() {
ifstream in("C:/C2Cpp/C19_Container/WordsCount/title.txt");
vector<string> words; //words[i]存放发现的第i个单词
vector<int> counts; //counts[i]存放words[i]在诗中的出现次数
string sWord;
while (in >> sWord){ //operator>>(in,sWord)
for (size_t i=0;i<words.size();i++){
if (words[i]==sWord){
counts[i]++;
sWord = "";
break;
}
}
if (sWord!=""){
words.push_back(sWord);
counts.push_back(1);
}
}
assert(words.size()==counts.size());//断言向量words及counts长度相等
for (size_t i=0;i<words.size();i++){
cout << words[i] << ":" << counts[i] << ", ";
}
return 0;
}
title.txt文内文件内容:
When you are old
--- William Butler Yeats
When you are old and grey and full of sleep,
And nodding by the fire, take down this book,
And slowly read, and dream of the soft look
Your eyes had once, and of their shadows deep;
How many loved your moments of glad grace,
And loved your beauty with love false or true,
But one man loved the pilgrim soul in you,
And loved the sorrows of your changing face;
And bending down beside the glowing bars,
Murmur, a little sadly, how Love fled
And paced upon the mountains overhead
And hid his face amid a crowd of stars.
请提交一个PDF文件:
1. 包含你的学号及姓名;
2. 你的程序源代码;
3. 运行结果截图。
不接受PDF以外的文件格式。答题可在Word中编辑,然后再另存为PDF。
### 感觉不会? 那试着听听**免费的B站网课**
[简洁的C和C++ - 重庆大学在线课程](https://www.bilibili.com/video/BV1it411d7zx/)
[Python编程基础及应用 - 重庆大学在线课程](https://www.bilibili.com/video/BV1kt411R7uW/)

答案: