C ++'使用空格作为分隔符' [英] C++ 'Using space as a delimiter'
本文介绍了C ++'使用空格作为分隔符'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C ++的入门者,正在尝试执行以下程序:
I am a complete beginner in C++, and am trying to do the following program:
- 从控制台读取一个句子。
- 使用空格字符作为分隔符,将该句子分解为单词。
- 遍历每个单词,如果单词是数字值,则将其值打印一倍,否则将单词打印出来,并在每行输出。
我真的不知道该怎么做。尤其是使用空格键作为分隔符。
I am really lost on how to do this. Especially using the space key as a delimiter.
推荐答案
可以有如下所示的内容:
Can have something like following :
使用 std :: stringstream
和 std: :getline
std::string str;
std::string temp;
std::getline(std::cin,str);
std::stringstream ss(str);
while(getline(ss,temp, ' ')) // delimiter as space
{
std::stringstream stream(temp);
if(stream >> val)
std::cout<<2*val<<std::endl;
else
std::cout<<temp<<std::endl;
}
请参见 演示
See DEMO
这篇关于C ++'使用空格作为分隔符'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文