zoukankan      html  css  js  c++  java
  • STL之sstream的用法

    STL之sstream的用法

    说在前面:

    库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本。注意,使用string对象来代替字符数组。这样可以避免缓冲区溢出的危险。而且,传入参数和目标对象的类型被自动推导出来,即使使用了不正确的格式化符也没有危险。

    使用时必须加上#include<sstream>

    • stringstream 对象用于输入一行字符串,以 空格 为分隔符把该行分隔开来

      #include <iostream>
      #include <stack>
      #include <sstream>
      using namespace std;
      int main()
      {
          string str= "hello world I am very happy!";                           
          stringstream sstream(str);                                              //sstream<<
          while (sstream)
          {
              string substr;
              sstream>>substr;
              cout << substr <<" ";    //也可vec.push_back(substr);
          } 
          return 0;
      }
      //  输出详解:
      hello world I am very happy!
      //  大家可能会问这有什么用,如果配上栈结构便能将这个逆序输出:
      #include <iostream>
      #include <stack>
      #include <sstream>
      using namespace std;
      int main()
      {
          string str= "hello world I am very happy !",tmp;                           
          stringstream sstream(str);                                            
          stack<string>  s;
          while(sstream>>tmp)
          {
              s.push(tmp);
          }
          while(!s.empty())
          {
              cout<<s.top();
              s.pop();
              if(s.size()!=0)
              cout<<" ";
          }
          cout<<endl;
          return 0;
      }
      //  输出结果:
      ! happy very am I world hello
      
    • 类型转换

      利用stringstream进行常见类型转换

      ​ (int、long、float、double、long double、long long)

    /-----string转int-----/
    string str= "123456789";                           
    stringstream sstream(str);                                            
    int tmp;
    sstream>>tmp;
    cout<<tmp<<endl;     //  输出结果123456789
    /-----int转string-----/
       int tmp=123456;
        stringstream t;
        string s;
        t<<tmp;
        t>>s;
        cout<<s<<endl;  //  输出结果123456
    /-----int转char-----/
        int tmp=123456;
        stringstream t;
        char s[10];
        t<<tmp;
        t>>s;
        for(int i=0;i<strlen(s);i++)
            cout<<s[i];   //  输出结果123456
    /-----char转int-----/
        stringstream t;
        char s='8';
        int tmp;
        t<<s;
        t>>tmp;
        cout<<tmp;
        return 0;  //  输出8;
    
    • 利用to_string() 进行常见类型转换, 返回值为string

      函数原型:(C++11才有这个函数)
      string to_string (int val);
      string to_string (long val);
      string to_string (long long val);
      string to_string (unsigned val);
      string to_string (unsigned long val);
      string to_string (unsigned long long val);
      string to_string (float val);
      string to_string (double val);
      string to_string (long double val);

      功能:

      将数值转化为字符串。返回对应的字符串。

      例子:

    • s1=to_string(10.5);                        //double到string
      s2=to_string(123);                        //int到string
      s3=to_string(true);                     //bool到string
      
      
      #include <iostream>
      #include <stack>
      #include <sstream>
      #include <cstring>
      #include <iomanip>
      using namespace std;
      int main()
      {
          string s1,s2,s3;
          s1=to_string(10.5); //double到string
          s2=to_string(123);//int到string
          s3=to_string(true);//bool到string
          cout<<fixed<<showpoint<<setprecision(1);
          cout<<s1<<" "<<s2<<" "<<s3<<endl;
          return 0;
      }
      //  10.500000 123 1
      
      
      重复利用stringstream对象

      如果你打算在多次转换中使用同一个stringstream对象,记住再每次转换前要使用clear()方法;

      在多次转换中重复使用同一个stringstream(而不是每次都创建一个新的对象)对象最大的好处在于效率。stringstream对象的构造和析构函数通常是非常耗费CPU时间的。

      #include <iostream>
      #include <stack>
      #include <sstream>
      #include <cstring>
      using namespace std;
      int main()
      {                                         
          stringstream stream;
          int first;
          string second;
          stream<< "456"; //插入字符串
          stream >> first; //转换成int
          cout << first << endl;
          stream.clear(); //在进行多次转换前,必须清除stream,否则第二个数据可能输出乱码
          stream << "true"; //插入bool值
          stream >> second; //提取出int
          cout << second << endl;
          return 0;
      }
      //  输出结果:
      456
      true
      
  • 相关阅读:
    win7下jdk安装环境变量配置
    !function($){}(window.jQuery)
    转载几篇
    YeeLink
    坑爹JDK8,可怜的XP
    一致性Hash
    CSS
    仿函数
    launch4j
    GOAL!!!!!!!!!!!!!
  • 原文地址:https://www.cnblogs.com/Yqifei/p/12660054.html
Copyright © 2011-2022 走看看