将输入作为C 中的单词数组
Taking input as array of words in c++
std::vector<std::string> words;
std::string word;
while (std::cin >> word)
words.push_back(word);
cout<<words[1];
我正在使用它进行输入来创建一个用whitespaces创建一系列单词。但是在使用Enter结束句子后,我没有得到任何输出。
我正在使用它进行输入来创建一个用whitespaces创建一系列单词。但是在使用Enter结束句子后,我没有得到任何输出。
那是因为
while (std::cin >> word)
words.push_back(word);
按下 Enter
时不会停止当std::cin
中没有更多数据时,它会停止。您必须输入EOF才能离开while
循环。
有用的链接:如何在此程序中输入EOF字符?。
使用
getline(cin,word)
而不是
cin<<word
将允许您输入带有空格的单词。
即使,向量开始于索引0,因此输出
words[0]
而不是
words[1]
将输出输入的值。这就是问题。
相关文章:
- 当您在此单词中搜索单词时调整字符数组的大小?
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 如何从文件中读取单词,将它们分配给数组并分析其内容?
- C++ 计算数组中的单词
- C ++程序在字符数组中插入单词
- 使用结构数组计算文本文件中单词的出现次数C++
- 将字节数组 (uint8_t) 转换为单词数组 (uint16_t),反之亦然
- 我需要C++读取字符串数组中每个单词的第一个字母
- 如何使数组以单词而不是数字的形式出现
- 在字符数组中存储和恢复一组单词
- 如何在字符指针数组中保留指向每个单词的指针?
- 如何输入每个单词在字符串数组中的句子,直到用户按Enter键
- 如何插入一个单词并将字母放在2D数组中的不同索引中
- 我可以在不循环访问数组/向量的情况下检查数组/向量中的单词吗?
- 我如何计算一个单词在数组中重复的次数
- char 数组在上次迭代时复制整个单词而不是单个字母
- 从字符数组中删除单词
- 如何将 c 字符串转换为指针数组,其中每个指针都是 char 数组中的一个单词
- 应用于单词数组的随机数范围