zoukankan      html  css  js  c++  java
  • C++: I/O流详解(三)——串流

    一、串流

    串流类是 ios 中的派生类

    C++的串流对象可以连接string对象或字符串

    串流提取数据时对字符串按变量类型解释;插入数据时把类型 数据转换成字符串

    串流I/O具有格式化功能

    例:

     1 #include <iostream>
     2 #include <sstream>
     3 #include <string>
     4 #include <strstream>
     5 
     6 using namespace std;
     7 //?什么鬼? 
     8 /*从输入串流对象提取数据 
     9 int main(){
    10     string testStr("Input test 256*0.5");
    11     string s1, s2, s3;
    12     double x, y;
    13     istringstream input(testStr); //用string对象构造串流对象
    14     input>> s1>> s2>> x>> s3>> y;
    15     cout<< s1<< ends<< s2<< ends<< x<< s3<< y<< "="<< x*y<< endl;
    16     return 0;
    17 }
    18 */
    19 
    20 //向输出串流对象插入数据
    21 /*
    22 int main(){
    23     ostringstream Output;
    24     double x, y;
    25     cout<< "Input x:";
    26     cin>> x;
    27     cout<< "Input y:";
    28     cin>> y;
    29     Output<<x<<"*"<<y<<"="<<x*y<<endl;
    30     cout<<Output.str();
    31     return 0;
    32 } 
    33 */
    34 
    35 //向输出串流对象插入数据
    36 int main(){
    37     char buf[80];
    38     ostrstream Output(buf, sizeof(buf));//对Output的数据成员初始化 
    39     double x, y;
    40         cout<< "Input x:";
    41     cin>> x;
    42     cout<< "Input y:";
    43     cin>> y;
    44     Output<<x<<"*"<<y<<"="<<x*y<<endl;
    45     cout<<Output.str();
    46     return 0;
    47 } 
  • 相关阅读:
    抓老鼠啊
    币值转换
    打印沙漏
    秋季学习总结
    第五周课程总结&试验报告(三)
    第四周课程总结&试验报告2
    实验报告一 &第三周课程总结
    Java第二周总结
    2019春总结作业
    第十六周
  • 原文地址:https://www.cnblogs.com/llxblogs/p/7768786.html
Copyright © 2011-2022 走看看