Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
我如何在c ++中接受字符串输入,如下所示:
从行首开始,终止于第一个空格,并忽略该行第一个空格旁边的内容,第二个字符串转到下一行,
输入来自标准输入
例:
字符串数组应为conatin
那么您可以根据需要加入他们。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
我如何在c ++中接受字符串输入,如下所示:
从行首开始,终止于第一个空格,并忽略该行第一个空格旁边的内容,第二个字符串转到下一行,
输入来自标准输入
例:
this is a sample input
here are few lines
looknospaces!
字符串数组应为conatin
string[0]=this
string[1]=here
string[2]=looknospaces!
最佳答案
也许一个好方法是使用getline然后提取每行中的第一个单词,否则我不太喜欢但可行的解决方案是(ab)使用getline分隔符
#include <string>
#include <iostream>
int main()
{
std::string name, discard;
std::getline(std::cin, name, ' '); // Takes just the first word (if present)
std::getline(std::cin, discard); // Takes the rest until \n, discard this
那么您可以根据需要加入他们。
关于c++ - 字符串输入从行的开头开始,以空格终止。,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22436652/