c++ - 使用 unordered_map 创建哈希函数以获取单词出现次数

标签 c++ hash

我有字符串 std::string str(s); 并且 s 有像这样的单词列表 s="一二三一一二..."

而且我希望每个词和末尾词的出现次数都达到最大值。

我已声明出现类型:

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

我想把s的内容赋值给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";
    }

任何人都可以告诉我如何在这里得到每个单词“one”、“two”的出现次数吗?

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

#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;
}

最佳答案

你有一个好的开始。而且您正在为您的计数器使用正确的类,这比大多数人都多。您需要的机制是能够从较大的字符串中解析出子字符串,其中空格是您的分隔符。 std::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

这应该可以帮助您入门。找到最大的计数对您来说应该没有问题(提示:每次插入后,您的 map 都有刚刚处理的单词的当前计数)。

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

关于c++ - 使用 unordered_map 创建哈希函数以获取单词出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18396417/

相关文章:

c++ - rpcrt4.dll 是否是开源/免费库?我可以将该库捆绑到我的应用程序吗

php - 如何使用 codeigniter 正确实现 PHPass 密码哈希?

ruby - 从哈希创建 CSV

python - Python 脚本是否可以对自身进行 md5 哈希?

java - 哈希算法将特定数量的数字放入固定数量的桶中

c++ - 如何反转 `std::integer_sequence<int, 4, -5, 7, -3>` 中整数的顺序?

c++ - 这是缩小数组的有效方法吗?

c++ - bool 必须只接受一个参数 == 运算符实现文件

c++ - 不支持的操作。 JRC 引擎处理的文档无法在 C++ 堆栈中打开

algorithm - 哈希数字向量的方法?