鱼C论坛

 找回密码
 立即注册
查看: 2931|回复: 2

求助大神啊 在线等

[复制链接]
发表于 2013-11-8 23:50:16 | 显示全部楼层 |阅读模式
5鱼币
111.jpg
最后一行输出的line的值为什么为空?

最佳答案

查看完整内容

看我的代码注释(望楼主以后发帖,学会帖代码呀,看截图写代码很浪费时间答题的):
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-11-8 23:50:17 | 显示全部楼层
看我的代码注释(望楼主以后发帖,学会帖代码呀,看截图写代码很浪费时间答题的):
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include<sstream>
  6. #include<cctype>

  7. using namespace std;
  8. int main()
  9. {
  10.         string line;
  11.         string line2="abc";
  12.         getline(cin,line);//将用户输入的字符串放到line中
  13.         istringstream hhh(line);//创建istringstream对象hhh
  14.         while(hhh>>line)//从hhh中以空格为界提取字符串放到line中,循环结束时line的值为空
  15.         {
  16.                 cout<<line2<<endl;
  17.                 cout<<line.c_str()<<endl;
  18.         }

  19.         cout<<"跳出循环了!"<<endl;
  20.         cout<<line<<endl;//如果line的值不为空,怎么可能跳出上面的while循环呢?

  21.         return 0;
  22. }

复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2013-11-9 13:36:22 | 显示全部楼层
我给你一个改进的例子代码吧:
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include<sstream>
  6. #include<cctype>

  7. using namespace std;
  8. int main()
  9. {
  10.         //istringstream对象可以绑定一行字符串,
  11.         //然后以空格为分隔符把该行分隔开来
  12.         string line="11 22 33 44 55";
  13.         string s;
  14.         istringstream hhh(line);
  15.         while(hhh>>s)
  16.         {
  17.                 cout<<s.c_str()<<endl;
  18.         }
  19.         cout<<"循环结束了!"<<endl;
  20.         cout<<line;
  21.         return 0;
  22. }
复制代码


效果截图:

1.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 23:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表