代码卡在循环中(但循环本身并没有继续)。C++;一串 我现在正在用Schaum的大纲书自学C++(主要包括C内容,或者我已经被告知,但是不管怎样),我遇到了问题9.8的一些麻烦。 你应该计算一个给定的C++字符串中每个单词的出现次数,我假设每个单词都是从一个空白、一个换行符或一个点或昏迷中分离出来的(后面这两个单词后面是另一个空白)。 我的代码如下: #include <iostream> #include <string> using namespace std; int main() { string s; cout << "Enter text (enter \"$\" to stop input):\n"; getline(cin,s,'$'); string s2 = s, word; int ini = 0, last, count_word = 0; int count_1 = 0, count_2 = 0, count_3 = 0; cout << "\nThe words found in the text, with its frequencies, are the following:\n"; for (ini; ini < s.length(); ) { // we look for the next word in the string (at the end of each iteration // ini is incremented in a quantity previous_word.length() last = ini; cout << "1: " << ++count_1 << endl; while(true) { if (s[last] == ' ') break; if (s[last] == '\n') break; if (s[last] == ',') break; if (s[last] == '.') break; if (last > s.length()-1 ) break; ++last; cout << "2: " << ++count_2 << endl; } --last; // last gives the position within s of the last letter of the current word // now me create the word itself word = s.substr(ini,last-ini+1); //because last-ini is word.length()-1 int found = s2.find(word); while( found != s2.length() ) // the loop goes at least once ++count_word; s2.erase(0,found+word.length()); // we erase the part of s2 where we have already looked found = s2.find(word); cout << "3: " << ++count_3 << endl; cout << "\t["<<word<<"]: " << count_word; ++last; s2 = s; s2.erase(0,ini + word.length()); // we do this so that in the next iteration we don't look for // the new word where we know it won't be. if (s[last] == ' ' || s[last] == '\n') ini = last + 1; if (s[last] == ',' || s[last] == '.') ini = last + 2; count_word = 0; } } #包括 #包括 使用名称空间std; int main() {字符串s; cout

代码卡在循环中(但循环本身并没有继续)。C++;一串 我现在正在用Schaum的大纲书自学C++(主要包括C内容,或者我已经被告知,但是不管怎样),我遇到了问题9.8的一些麻烦。 你应该计算一个给定的C++字符串中每个单词的出现次数,我假设每个单词都是从一个空白、一个换行符或一个点或昏迷中分离出来的(后面这两个单词后面是另一个空白)。 我的代码如下: #include <iostream> #include <string> using namespace std; int main() { string s; cout << "Enter text (enter \"$\" to stop input):\n"; getline(cin,s,'$'); string s2 = s, word; int ini = 0, last, count_word = 0; int count_1 = 0, count_2 = 0, count_3 = 0; cout << "\nThe words found in the text, with its frequencies, are the following:\n"; for (ini; ini < s.length(); ) { // we look for the next word in the string (at the end of each iteration // ini is incremented in a quantity previous_word.length() last = ini; cout << "1: " << ++count_1 << endl; while(true) { if (s[last] == ' ') break; if (s[last] == '\n') break; if (s[last] == ',') break; if (s[last] == '.') break; if (last > s.length()-1 ) break; ++last; cout << "2: " << ++count_2 << endl; } --last; // last gives the position within s of the last letter of the current word // now me create the word itself word = s.substr(ini,last-ini+1); //because last-ini is word.length()-1 int found = s2.find(word); while( found != s2.length() ) // the loop goes at least once ++count_word; s2.erase(0,found+word.length()); // we erase the part of s2 where we have already looked found = s2.find(word); cout << "3: " << ++count_3 << endl; cout << "\t["<<word<<"]: " << count_word; ++last; s2 = s; s2.erase(0,ini + word.length()); // we do this so that in the next iteration we don't look for // the new word where we know it won't be. if (s[last] == ' ' || s[last] == '\n') ini = last + 1; if (s[last] == ',' || s[last] == '.') ini = last + 2; count_word = 0; } } #包括 #包括 使用名称空间std; int main() {字符串s; cout,c++,c,string,loops,output,C++,C,String,Loops,Output,对于一个非常简单的问题来说,这是一个非常复杂的方法。您可以使用stringstream提取每个由空格分隔的单词。然后您只需提取提取的单词,并使用std::map递增单词计数器 我的看法是: #include <iostream> #include <map> #include <string> #include <sstream> int main() { std::map<std::string, int> word_to_

对于一个非常简单的问题来说,这是一个非常复杂的方法。您可以使用
stringstream
提取每个由空格分隔的单词。然后您只需提取提取的单词,并使用
std::map
递增单词计数器

我的看法是:

#include <iostream>
#include <map>
#include <string>
#include <sstream>

int main() {
    std::map<std::string, int> word_to_count;
    std::string in;
    std::getline(std::cin, in);
    
    std::stringstream s(in);
    std::string temp_word;
    while (s >> temp_word) {
        word_to_count[temp_word]++; 
    }
    
    for (const auto& x : word_to_count) {
        std::cout << x.first << ": " << x.second << std::endl;
    }
    return 0;
}
输出
请记住,这只是许多可能的解决方案之一,因此请将此作为灵感:)。

如果您在调试器下运行程序员,当它“卡在循环中”时,您可以点击“暂停”。这将告诉您需要知道的一切。您的
while(find!=s2.length())
循环缺少大括号,因此它将只运行下一行(
++count\u word;
)在循环中,而不是所有缩进的语句。由于
++count\u word
永远无法更改
found
的值,循环将永远继续。我已经检查了第二个while循环中是否没有大括号,这很好。问题仍然存在于第三个循环中,但我找不到它。
hello world hello world test 
hello: 2
test: 1
world: 2