1

我有字符串std::string str(s);,s 有类似的单词列表 s="one two three one one two..."

而且我希望每个单词的出现以及具有最大出现值的结尾单词。

我已经声明了发生类型:

typedef std::unordered_map<std::string> occurrences;
occurrences s1;

我想分配sinto的内容s1,我该怎么做?

之后,这里是获取每个单词出现错误的代码:

for (std::unordered_map<std::string, int>::iterator it = s1.begin();
                                                    it != s1.end();
                                                    ++it)
    {
        std::cout << "word :" << it->first << "occured   " << it->second <<  "  times \n";
    }

谁能告诉我如何在这里出现每个单词“一”、“二”?

根据要求,我在这里添加原始代码:

#include <string>
#include <iostream>
#include <unordered_map>

int main()
{
    typedef std::unordered_map<std::string,int> occurrences;
    occurrences s1;
    s1.insert(std::pair<std::string,int>("Hello",1));
    s1.insert(std::pair<std::string,int>("Hellos",2));

    for (std::unordered_map<std::string, int>::iterator it = s1.begin();it != s1.end();++it)
    {
        std::cout << "word :" << it->first << "occured   " << it->second <<  "  times \n";
    }

    return 0;
}

改进的代码:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <unordered_map>

int main()
{
    typedef std::unordered_map<std::string,int> occurrences;
    occurrences s1;

    // the string we're splitting.
    std::string s = "one two three one one two";

    int maxcount=0,temp=0;  
    std::vector<std::string> vestring;

    // create an input string stream
    std::istringstream iss(std::move(s));

    // now simply extract strings until you reach end-of-file.
    while (iss >> s)
    {
        temp=++s1[s];
    if(temp>=maxcount)
    {
        maxcount=temp;
        vestring.push_back(s);
    }

    }
    for (occurrences::const_iterator it = s1.cbegin();it != s1.cend(); ++it)
        std::cout << it->first << " : " << it->second << std::endl;;

    return 0;
}
4

1 回答 1

2

你有一个好的开始。而且您正在为您的计数器使用正确的类,这比大多数人都多。您需要的机制是能够从较大的字符串中解析出子字符串,空格是您的分隔符。Astd::istringstream<>会很好地做到这一点。

样本

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <unordered_map>

int main()
{
    typedef std::unordered_map<std::string,int> occurrences;
    occurrences s1;

    // the string we're splitting.
    std::string s = "one two two three one one two";

    // create an input string stream. we use std::move() to give
    //  the implementation the best chance at simply reassigning
    //  the buffer from the string to the stream
    std::istringstream iss(std::move(s));

    // this will hold all occurrances of the strings with
    //  maximum count. each time a new maximum is found
    //  we clear the vector and push the new leader in.
    std::vector<std::string> most;
    int max_count = 0;

    // now simply extract strings until you reach end-of-file.
    while (iss >> s)
    {
        int tmp = ++s1[s];
        if (tmp == max_count)
        {
            most.push_back(s);
        }
        else if (tmp > max_count)
        {
            max_count = tmp;
            most.clear();
            most.push_back(s);
        }
    }

    // send our map to stdout.
    for (occurrences::const_iterator it = s1.cbegin();it != s1.cend(); ++it)
        std::cout << it->first << " : " << it->second << std::endl;;

    // send the strings with max_count to stdout
    std::cout << std::endl << "Maximum Occurrences" << std::endl;
    for (std::vector<std::string>::const_iterator it = most.cbegin(); it != most.cend(); ++it)
        std::cout << *it << std::endl;

    return 0;
}

输出

three : 1
two : 3
one : 3

Maximum Occurrences
one
two

这应该让你开始。找到最大的计数对您来说应该没有问题(提示:每次插入后,您的地图都有刚刚处理的单词的当前计数)。

有更有效的方法可以做到这一点,但对于你现在正在做的事情来说可能绰绰有余。

于 2013-08-23T06:57:16.507 回答