使用向量计算元素的出现次数 我现在正在学习C++(适当的),通过安德鲁·克尼格和Barbara Moo自己的加速C++来做,并且在每章中做所有的练习。< /P>

使用向量计算元素的出现次数 我现在正在学习C++(适当的),通过安德鲁·克尼格和Barbara Moo自己的加速C++来做,并且在每章中做所有的练习。< /P>,c++,vector,C++,Vector,练习3-3:编写一个程序,计算每个不同单词在输入中出现的次数。对我来说,这项练习似乎非常困难,特别是考虑到:1。该章中的示例和其他练习相对简单且简单。您只允许使用向量,因此没有任何高级功能。(或者只是我对困难的判断有误) 我在网上搜索了一些提示,看到其他人在这个练习中遇到了问题,但人们提供的解决方案对我来说似乎不清楚。大多数人建议使用本书后面介绍的组织方法,这与练习的要点背道而驰。最后,我将在不同论坛(包括这里)上找到的一些提示和方法拼凑起来,提出了自己的解决方案: #include <a

练习3-3:编写一个程序,计算每个不同单词在输入中出现的次数。对我来说,这项练习似乎非常困难,特别是考虑到:1。该章中的示例和其他练习相对简单且简单。您只允许使用向量,因此没有任何高级功能。(或者只是我对困难的判断有误)

我在网上搜索了一些提示,看到其他人在这个练习中遇到了问题,但人们提供的解决方案对我来说似乎不清楚。大多数人建议使用本书后面介绍的组织方法,这与练习的要点背道而驰。最后,我将在不同论坛(包括这里)上找到的一些提示和方法拼凑起来,提出了自己的解决方案:

#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;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cin;
使用std::setprecision;
使用std::cout;
使用std::string;
使用std::endl;
使用std::streamsize;
使用std::sort;
使用std::vector;
int main()
{
//请求字符串输入
cout(单词)
{
文字输入。推回(文字);
}
//按字母顺序对向量排序,以便能够分离不同的单词
排序(word_input.begin(),word_input.end());
//创建两个向量:一个是每个(字符串)元素都是唯一单词的向量,另一个是
//存储新单词出现的索引的
向量唯一词;
向量break_指数;
对于(int i=0;i!=word_input.size()-1;++i)
{
如果(字输入[i+1]!=字输入[i])
{
唯一的单词。向后推(单词输入[i]);
破坏指数。将指数推回(i);
}
}
//将序列中的最后一个单词添加到唯一的单词字符串向量
唯一的单词。向后推(单词输入[单词输入.大小()-1]);
//创建一个向量,计算每个唯一单词出现的次数,预先分配
//一个新词在系列中出现的次数与1相同(加1计算第一个词)
向量字计数(1,中断索引[0]+1);
//如果一个新词出现了,通过减去到目前为止的单词数量来计算上一个词出现的次数
对于(int i=0;i!=break_index.size()-1;++i)
{
单词计数。向后推(break_索引[i+1]-break_索引[i]);
}
//添加序列中最后一个单词出现的次数:文本的总大小-1(索引从0开始)-最后一个单词开始的索引
word_count.push_back(word_input.size()-1-break_index[break_index.size()-1]);
//(不同的)字数及其频率输出

cout如果你想象有人在用你的代码处理莎士比亚的全部作品,那么你存储每个单词会浪费很多空间。如果你使用“单词”和“单词计数”的结构,你只需要存储单词“the”一次,即使它在你的程序输入的文本中出现100000次。也就是说,如果你甚至需要知道这个单词已经出现了不止一次-如果你只需要一个唯一单词的列表,你只需要看看你是否已经存储了这个单词。[按顺序存储它们可以使用
binary\u search
查找它们,如果您确实通过代码运行莎士比亚的800K(不是唯一的)单词,这将有助于运行时]

解决方案对我有效(当我处理此问题时)was将使用三个向量:一个
输入向量
、一个
输出向量
、一个
计数向量
。使用
std::cin
读取用户输入,直到输入转义字符:使用
输入向量。推回(输入字)
用单词填充
输入向量
。使用
标准::排序
对向量进行排序,并创建
输出向量
(一个值为
输入向量
中的第一个单词)和
计数向量
(一个值为
1


然后,对于输入向量中的每个元素(从第二个开始,而不是第一个),检查当前元素是否与最后一个元素相同。如果是,请将
1
添加到
count\u vector
中的当前元素中。否则,请使用
push\u back()
input\u vector
中的当前单词添加到
output\u vector
中,并将
count\u vector
的大小增加一个元素(其值为
1
)。

这更适合。Stackoverflow不是一个代码审查站点。我的意见是,您在运算符周围缩进和使用空格的方式非常不一致。您可以通过使用std::unique来整合一些工作,从中删除两个迭代器之间连续的非唯一元素(这将从单词列表中删除重复项)好吧,只是非常快速地扫描代码,您为此花费了相当多的逻辑。它应该只是一个容器(列表/向量),一个循环遍历所有单词,如果当前容器中已经包含单词,则在当前容器上使用
if(std::find())
(如果没有,请随意插入)。总字数可以通过container.size()轻松检索,并且不能单独保存(不是说这会带来很多伤害,只是不必要)。问题是
O(n)
难,其中n是输入字数。您的解决方案是
O(nlogn)
因此,您的解决方案不是最优的。您可以实现最优的
O(n)
但这意味着执行与
std::map uniqueWords
等效的操作。存在人工“仅矢量”限制。因此,只要您只需要“我得到了哪些单词”,您仍然可以使用向量上的
find
,查看您以前是否见过这个单词。而不是存储所有单词,然后删除重复的单词。是的,