sstream

比较常用的是这个 stringstream,用来转换数据贼方便,比起sscanf和sprintf来说更好用。

<sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。

  1.stringstream::str(); returns a string object with a copy of the current contents of the stream.

  2.stringstream::str (const string& s); sets s as the contents of the stream, discarding any previous contents.

  3.stringstream清空,stringstream s; s.str("");

  4.实现任意类型的转换

    template<typename out_type, typename in_value>
    out_type convert(const in_value & t){
      stringstream stream;
      stream<<t;//向流中传值
      out_type result;//这里存储转换结果
      stream>>result;//向result中写入值
      return result;
    }

 

对于stringstream判断空,可以if(stringstream>>str){//进入这里说明不为空,反之亦然}

posted @ 2019-12-27 09:51  *zzq  阅读(235)  评论(0编辑  收藏  举报