C++ 计算文本文件中出现的单词数

C++ 计算文本文件中出现的单词数,c++,C++,这是我基于这里的代码。(c++中新增) #包括 #包括 #包括 使用名称空间std; int main() { //std::cout使用std::string的解决方案 int count = 0; std::string word_to_find, word_inside_file; std::ifstream fin("my_data.txt"); std::cout << "Enter a word to count:"; std::cin >> word_to

这是我基于这里的代码。(c++中新增)

#包括
#包括
#包括
使用名称空间std;
int main()
{

//std::cout使用std::string的解决方案

int count = 0;
std::string word_to_find, word_inside_file;

std::ifstream fin("my_data.txt");
std::cout << "Enter a word to count:";
std::cin >> word_to_find;
while (fin >> word_inside_file) {
    if (word_to_find == word_inside_file )
        count++;
}

std::cout << "Occurrence=" << count << "";

内部>代码> FIDAL LoopCurrices(STD::String,STD::String)。您将实现“在另一个字符串中查找所有字符串出现”算法。

< P>如果您是C++的新成员,则不应该使用GET。请阅读“缓冲区溢出漏洞”。更像是C风格。你应该考虑使用STD::CIN。< /P>你可以共享MyAdDATA?TXT的内容吗?(或者至少一部分,如果它太大了)当然,当你在调试器中通过它时,会更新问题,它是怎么说的?你是不是已经预料到了?既然你使用C++,你会考虑使用STD::string吗?读一下如何构造一个输入循环HI,这个方法很好,但是当我在单词之间没有空格时,它就不再读C了。恰如其分地像“世界”因为fin>>word\u在\u文件中会一次读取一个完整的字符串,在本例中是WorldWorld,它与world.right不同。这就是我在上一个示例中使用char的原因,在没有空间的情况下,有没有办法做到这一点?您可以使用std::string::find和std::string::substr,但您必须实现一些算法t这里因为find只返回第一次出现。这是一个问题,在另一个字符串中查找所有出现的字符串。根据这个问题,我接受这个答案。谢谢
get
六年前已从C中删除。请参考?。注意“直到C11”标签。
gets
一直被弃用,最后在C11中被删除。
int count = 0;
std::string word_to_find, word_inside_file;

std::ifstream fin("my_data.txt");
std::cout << "Enter a word to count:";
std::cin >> word_to_find;
while (fin >> word_inside_file) {
    if (word_to_find == word_inside_file )
        count++;
}

std::cout << "Occurrence=" << count << "";
...
while (fin >> word_inside_file) {
    count += findAllOccurrences(word_to_find, word_inside_file);
}
...