gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:15:20 25 4
gpt4 key购买 nike

我有字符串 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com