gpt4 book ai didi

c++ - 使用字符串流提取参数

转载 作者:行者123 更新时间:2023-11-30 02:52:25 27 4
gpt4 key购买 nike

我想输入一个短语并提取短语的每个字符:

int main()
{
int i = 0;
string line, command;
getline(cin, line); //gets the phrase ex: hi my name is andy
stringstream lineStream(line);
lineStream>>command;
while (command[i]!=" ") //while the character isn't a whitespace
{
cout << command[i]; //print out each character
i++;
}
}

但是我得到错误:无法在 while 语句中比较指针和整数

最佳答案

正如您的标题“Extracting arguments using stringstream”所暗示的那样:

我想你正在寻找这个:

getline(cin, line); 
stringstream lineStream(line);

std::vector<std::string> commands; //Can use a vector to store the words

while (lineStream>>command)
{
std::cout <<command<<std::endl;
//commands.push_back(command); // Push the words in vector for later use
}

关于c++ - 使用字符串流提取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18947404/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com