zoukankan      html  css  js  c++  java
  • sstream

    istringstream类用于执行C++风格的串流的输入操作。是由一个string对象构造而来,从一个string对象读取字符。 

    ostringstream类用于执行C风格的串流的输出操作。 同样是有一个string对象构造而来,向一个string对象插入字符。

    stringstream则是用于C++风格的字符串的输入输出的。 同时可以支持C风格的串流的输入输出操作。

    初始化方法:

    1. stringstream stream;
      int a;
      stream << "12";
      stream >> a;
      cout << a << endl;
    2. stringstream stream("adfaf afagfagag");
      string a;
      stream >> a;
      cout << a << endl;
    3. string a = "aljfljfalf";
      stringstream stream(a);
      cout << stream.str() << endl;
     1 #include <sstream>
     2 using namespace std;
     3 int main()
     4 {
     5     stringstream stream;
     6     int a,b;
     7     stream << "080";//将“080”写入到stream流对象中
     8     stream >> a;//将stream流写入到a中,并根据a的类型进行自动转换,"080" -> 80
     9     cout << stream.str() << endl;//成员函数str()返回一个string对象,输出80
    10     cout << stream.length() << endl;//2
    11 }
     1 #include <sstream>
     2 using namespace std;
     3 int main()
     4 {
     5     stringstream stream("123 3.14 hello");//不同对象用空格隔开,默认根据空格进行划分  
     6     double a;
     7     double b;
     8     string c;
     9     //将对应位置的内容提取出来写到对应的变量中
    10     //如果类型不同,自动转换
    11     stream >> a >> b >> c;//输出 1233.14hello
    12     cout << a << b << c;
    13     stream.clear();
    14     return 0;
    15 }

    str()和clear()

    stream用完之后,其内存和标记仍然存在,需要用这两个函数来初始化。

    clear()只是清空而是清空该流的错误标记,并没有清空stream流(没有清空内存);

    str(“”)是给stream内容重新赋值为空。

    一般clear和str(“”“”)结合起来一起用。

     1 #include <sstream>
     2 #include <cstdlib>//用于system("PAUSE")
     3 using namespace std;
     4 int main()
     5 {
     6     stringstream stream;
     7     int a,b;
     8     stream << "80";
     9     stream >> a;
    10     cout << stream.str() << endl;//此时stream对象内容是"80",不是空的
    11     cout << stream.str().length() << endl;//此时内存为2byte
    12     
    13     //此时再将新的"90"写入进去的话,会出错的,因为stream重复使用时,没有清空会导致错误。
    14     //如下:
    15     stream << "90";
    16     stream >> b;
    17     cout << stream.str() << endl;//还是80
    18     cout << b << endl;//1752248,错了,没有清空标记,给了b一个错误的数值
    19     cout << stream.str().length() << endl;//还是2bytes
    20     
    21     //如果只用stream.clear(),只清空了错误标记,没有清空内存
    22     stream.clear();
    23     stream << "90";
    24     stream >> b;
    25     cout << b << endl;//90
    26     cout << stream.str() <<endl;//变成90
    27     cout << stream.str().length() << endl;//4,因为内存没有释放,2+2 = 4bytes
    28     
    29     //stream.clear() 和 stream.str("")都使用,标记和内存都清空了
    30     stream << "90";
    31     stream >> b;
    32     cout << b << endl;
    33     cout << stream.str() << endl;//90
    34     cout << stream.str().length() << endl;//2bytes
    35     
    36     system("PAUSE");
    37     return 0;
    38 }
  • 相关阅读:
    redis 学习(17) -- RDB
    51单片机程序技巧
    无效设备解决办法
    210板子启动笔记
    RFID读卡器设置卡
    Socket简介
    /etc/hosts.conf
    TVP5150摄像头
    maven小试牛刀
    2014图灵技术图书最受欢迎TOP15
  • 原文地址:https://www.cnblogs.com/pacino12134/p/11033022.html
Copyright © 2011-2022 走看看