我目前正在通过自己阅读 Andrew Koenig 和 Barbara Moo 所著的 Accelerated C++ 一书(正确地)学习 C++,并完成每章中的所有练习。
练习 3-3:编写一个程序来计算每个不同单词在其输入中出现的次数。对我来说,这个练习似乎非常困难,特别是考虑到:1.那一章的例子和其他练习相对简单,2.你只允许使用向量,所以没有什么高级的。(或者也许只是我误判了难度)
我在网上搜索提示并看到其他人在此练习中遇到问题,但人们提供的解决方案对我来说似乎不清楚。大多数人建议使用本书后面介绍的组织方法,这与练习的要点相悖。最后,我将我在不同论坛(包括这里)上找到的提示和方法拼凑起来,提出了我自己的解决方案:
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::setprecision;
using std::cout;
using std::string;
using std::endl;
using std::streamsize;
using std::sort;
using std::vector;
int main()
{
// Ask for string input
cout << "Please write some text, followed by end-of-file: " << endl;
vector<string> word_input;
string word;
// input words into string vector word_input
typedef vector<string>::size_type vecsize;
while (cin >> word)
{
word_input.push_back(word);
}
// sort the vector in alphabetical order to be able to separate distinct words
sort(word_input.begin(),word_input.end());
// create two vectors: one where each (string) element is a unique word, and one
// that stores the index at which a new distinc word appears
vector<string> unique_words;
vector<int> break_index;
for (int i=0; i != word_input.size()-1; ++i)
{
if(word_input[i+1] != word_input[i])
{
unique_words.push_back(word_input[i]);
break_index.push_back(i);
}
}
// add the last word in the series to the unique word string vector
unique_words.push_back(word_input[word_input.size()-1]);
// create a vector that counts how many times each unique word occurs, preallocate
// with 1's with as many times a new word occurs in the series (plus 1 to count the first word)
vector<int> word_count(1,break_index[0]+1);
// if a new word occurs, count how many times the previous word occured by subtracting the number of words so far
for(int i=0; i != break_index.size()-1;++i)
{
word_count.push_back(break_index[i+1] - break_index[i]);
}
// add the number of times the last word in the series occurs: total size of text - 1 (index starts at 0) - index at which the last word starts
word_count.push_back(word_input.size()-1-break_index[break_index.size()-1]);
// number of (distinct) words and their frequency output
cout << "The number of words in this text is: " << word_input.size() << endl;
cout << "Number of distinct words is: " << unique_words.size() << endl;
// The frequency of each word in the text
for(int i=0; i != unique_words.size(); ++i)
cout << unique_words[i] << " occurs " << word_count[i] << " time(s)" << endl;
return 0;
}
有没有更好的方法使用向量来做到这一点?可以通过组合任何循环使代码更高效吗?