LintCode-1173 · 反转字符串 III-题解(istringstream简单使用)

题目链接:
https ://www.lintcode.com/problem/1173/?_from=collection&fromId=208

描述:
给定一个字符串句子,反转句子中每一个单词的所有字母,同时保持空格和最初的单词顺序。

样例:
输入 : Let's take LeetCode contesc
输出 : s'teL ekat edoCteeL tsetnoc

解题思路:
由样例可以看出先将字符串按空格分开后再进行反转,所以我们可以先将字符串按空格分割,
在C++中可以使用istringstream进行分割,用string的reverse(s.begin(), s.end())进行反转。

istringstream简单使用:
1、使用前必须包含头文件 < sstream>
2、构造istringstream:istringstream 变量名称(字符串 / 字符串变量);
3、样例

成都创新互联-专业网站定制、快速模板网站建设、高性价比大姚网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式大姚网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖大姚地区。费用合理售后完善,10多年实体公司更值得信赖。

#include  
#include        //istringstream 必须包含这个头文件
#include  
using namespace std;  
int main()  
{  
    string str="123 456\n789";  
    istringstream is(str);  
    string s;  
    while(is>>s)  // >> 按照字符流读入 所以是按' '或者'\n'分割
    {  
        cout<

网页题目:LintCode-1173 &#183; 反转字符串 III-题解(istringstream简单使用)
网站路径:http://myzitong.com/article/dsogoid.html