开发者

Count how many times each distinct word appears in input

开发者 https://www.devze.com 2023-01-18 14:11 出处:网络
I\'m working on an exercise from Accelerated C++: Write a program to count how many times each distinct word appears in its input.

I'm working on an exercise from Accelerated C++:

Write a program to count how many times each distinct word appears in its input.

Here's my code:

#include <iostream>
#include <string>
#include <vector>

int main()
{
    // Ask for 
    // and read the input words
    std::cout << "Please input your words: " << std::endl;
    std::vector<std::string> word_input;
    std::string word;
    int count = 0;
    while (std::cin >> word)
    {
        word_input.push_back(word);
        ++count;
    }

    // Compare the input words 
    // and output the times of every word compared only with all the words

    /***** I think this loop i开发者_如何学Pythons causing the problem ******/
    for (int i = 0; i != count; ++i)
    {
        int time = 0;
        for (int j = 0; j != count; ++j)
        {
            if (word_input[i] == word_input[j])
                ++time;
            else
                break;
        }

        std::cout << "The time of "
                    << word_input[i]
                    << " is: "
                    << time
                    << std::endl;
    }

    return 0;   
}

If you compile and run this program, you will see:

Please input your words:

And I input as follows:

good good is good
EOF

Then it shows:

The time of good is: 2
The time of good is: 2
The time of is is: 0
The time of good is: 2

My expected result is:

The time of good is: 3
The time of is is: 1

I don't want to use a map, because I haven't learned that yet.

What is causing this unexpected behavior, and how do I fix it?


Assuming that std::vector is the only container you're acquainted with at this point, and that you haven't gotten to std::pair yet, I suggest the following:

  • you add a std::vector<int> word_count
  • in your std::cin while-loop, you check if the current word is present in word_input. If it is not, you push_back the word and push_back a 1 in word_count. If there already is an entry for the current word at some index i in word_input, you increment word_count at this index i. Thus every distinct word you input only appears once in word_input, with the number of times it was input being managed in word_count.
  • for output, step through word_input and word_count in parallel and output the word count for every word.

Done.

But all this gets much simpler and more elegant with a std::map. Keep reading! :-)


Just delete the else statement.

int main()
{
    // Ask for 
    // and read the input words
    std::cout << "Please input your words: "
              << std::endl;
    std::vector<std::string> word_input;
    std::string word;
    int count = 0;
    while (std::cin >> word)
        {
            word_input.push_back(word);
            ++count;
        }

    // Compare the input words 
    // and output the times of every word compared only with all the words
    for (int i = 0; i != count; ++i)
        {
            int time = 0;
            for (int j = 0; j != count; ++j)
                {
                    if (word_input[i] == word_input[j])
                        ++time;
                    // else          <========== You don't need this!
                    //    break;
                }

            std::cout << "The time of "
                 << word_input[i]
                 << " is: "
                 << time
                 << std::endl;
        }

    return 0;   
}

Note that your solution is very slow for larger inputs. The better idea would be to use hashtable(std::map) for your 'dictionary' or to sort that vector and than count distinct words (runs in O(logN*N), your solution is O(N^2)).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号