I/O流类库(四)
2016-01-05 15:39
218 查看
字符串流
istringstream,由istream派生而来,提供读string的功能ostringstream,由ostream派生而来,提供些string的功能
stringstream,由iostream派生而来,提供读写string的功能
istringstream
#include <iostream> #include<sstream> using namespace std; int main(void) { string line; string word; while (getline(cin, line)) { istringstream iss(line); while (iss >> word) cout << word << "#"; cout << endl; } }
ostringstream
#include <iostream> #include<sstream> using namespace std; string Double2Str(double& val) { ostringstream oss; oss << val; return oss.str(); } double Str2Double(const string& str) { istringstream iss(str); double val; iss >> val; return val; } int main(void) { double val = 55.55; string str = Double2Str(val); cout << str << endl; str = "123.123"; val = Str2Double(str); cout << val << endl; return 0; }
/*-------------------例-----------------------------*/ #include <iostream> #include<sstream> using namespace std; int main(void) { istringstream iss("192,168,0,100"); cout << iss.str() << endl; int v1, v2, v3, v4; char ch; iss >> v1 >> ch >> v2 >> ch >> v3 >> ch >> v4; ostringstream oss; ch = '.'; oss << v1 << ch << v2 << ch << v3 << ch << v4; cout << oss.str() << endl; return 0; }
相关文章推荐
- [python] 小程序
- CC2538之TinyOS例程实验:10-CoAPServer zigbee灯光无线控制实验
- 如何提高oracle的查询速度(详解)
- jsp九大内置对象
- 坎坷路:ASP.NET 5 Identity 身份验证(上集)
- easyui tree onloadsuccess事件的心得
- js如何打印object对象
- PHP验证码
- (转)Apache2 httpd.conf 配置详解 (二)
- 分享使用Entity Framework的一个坑:Include无效
- LeetCode 23 - Merge k Sorted Lists
- 《thinking-in-java》读书笔记-第15章-泛型(一)
- 2、MSSqlServer常用操作(二)
- Android 5.0——Material Design详解(动画篇)
- android 获取状态栏高度
- 截屏相关
- MongoDB基本命令用
- Ubuntu下Eclipse搭建python,opencv开发环境
- warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
- 老程序员推荐的 10 个编程策略