股票数据格式 外汇交易系统 MT4编程 电子海图开发(S52 S57) AIS

http://alantop.5166.info

股票数据格式 外汇交易系统 MT4编程 电子海图开发(S52 S57) AIS
随笔 - 240, 文章 - 1, 评论 - 413, 引用 - 0
数据加载中……

stringstream的用法

stringstream通常是用来做数据转换的。

相比c库的转换,它更加安全,自动和直接。

 

例子一:基本数据类型转换例子 int转string

 

#include <string>
#
include <sstream>
#
include <iostream> 

int main()
{
    std
::stringstream stream;
    std
::string result;
    int i 
= 1000;
    stream 
<< i; //将int输入流
    stream >> result; //从stream中抽取前面插入的int值
    std::cout << result << std::endl; // print the string "1000"

 

 

运行结果:

001

 

例子二:除了基本类型的转换,也支持char *的转换。

 

#include <sstream>
#
include <iostream> 

int main()
{
    std
::stringstream stream;
    char result[
8] ;
    stream 
<< 8888//向stream中插入8888
    stream >> result; //抽取stream中的值到result
    std::cout << result << std::endl; // 屏幕显示 "8888"

 

 

002

 

例子三:再进行多次转换的时候,必须调用stringstream的成员函数clear().

 

#include <sstream>
#
include <iostream>
int main()
{
    std
::stringstream stream;
    int first
, second;
    stream
<< "456"//插入字符串
    stream >> first; //转换成int
    std::cout << first << std::endl;
    stream
.clear(); //在进行多次转换前,必须清除stream
    stream << true//插入bool值
    stream >> second; //提取出int
    std::cout << second << std::endl;

 

运行clear的结果

003

没有运行clear的结果

004

posted on 2007-07-10 19:07 AlanTop 阅读(1777) 评论(0)  编辑 收藏 引用 所属分类: C++


标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]




股票数据格式 外汇交易系统 MT4编程 电子海图开发(S52 S57) AIS