如何按行读取输入,然后按单词分隔
How do I read input by line and then separate by word?
我正试图从用户那里获取一个短语,并颠倒单词的顺序,然后将其打印出来。例如,"hello-world"变为"world-hello"。
我在这里发现了其他类似于我的问题,大多数"投票支持"的答案都建议这样做:
std::list<std::string> input;
std::list<std::string>::iterator iter;
std::string phrase;
std::string word;
std::cout << " Enter the phrase you wish to reverse " << std::endl;
std::cout << " >> ";
std::getline(std::cin, phrase);
std::istringstream iss(phrase);
while (iss >> word) {
input.push_front(word);
}
for (iter = input.begin(); iter != input.end(); ++iter) {
std::cout << *iter << " ";
}
然而,这对我不起作用。当我运行代码时,它从未停止允许输入。我不明白为什么它不允许我输入。
我该如何做到这一点,这样我就可以输入一个短语,并让程序逐字逐句地阅读它?
编辑:我使用的是MS Visual Studio 2015,并使用debug(f5)选项进行编译。
原来在输入流中有一个杂散"\n",我只是简单地调用了
std::cin.get();
以摆脱它。
我认为可能是这样,但我想不出如何"刷新"输入流,因为cin没有刷新方法。
非常感谢Ankur Jyoti Phukan的帮助!
相关文章:
- 文本文件中的单词链表
- 如何将 txt 文件中的行分隔为组件C++
- 无法通过空白将文本文件行分隔为矢量
- 在指针的帮助下,文本文件中单词的频率
- 如何根据单词在文本中出现的概率输出单词
- 如何在C++中读取空格分隔的输入 当我们不知道输入的数量时
- 从字符串变量中逐字符读取单词
- 使用std::mt19937从字符串中返回一个随机单词
- 如何用for循环在c++中生成单词三角形
- 将数字转换为单词
- 使用if-else将数字转换为单词
- 如何逐行从文件中获取单词,并在C++中用分号分隔?
- 包含换行符分隔的单词的文件和C++中这些单词的字符串向量的大小是否相同?
- 匹配由符号分隔的重复单词,忽略空格
- 字符串流代码分隔单词 - 这个东西是如何工作的?里面的代码片段
- C++:使用fgetc读取csv文件,并在分号上";"分隔单词
- 如何按行读取输入,然后按单词分隔
- 将用户输入的前两个单词分隔一个空格,然后存储剩余短语(C++)
- c++从文件中读取用空格和新行分隔的单词
- 如何创建一个字符串数组来拆分字符,其中单词由" "分隔?C++