• stringstream 类型转换


    stringstream可以吞下不同的类型,然后吐出不同的类型。

    这样可以实现int,string,double等类型的转换

     1 #include<sstream>
     2 
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     int x;
     8     stringstream temp;
     9     temp << x;//吞入被转换的类型
    10     string s1 = temp .str();//吐出需要的类型
    11 }
    12     

    缺点:效率低,运行时间长;由于cin,cout为了兼容C有个同步流。关闭同步流的方法

    看来stringstream似乎不打算主动释放内存(或许是为了提高效率),但如果你要在程序中用同一个流,反复读写大量的数据,将会造成大量的内存消耗,因些这时候,需要适时地清除一下缓冲 (用 stream.str("") )

    另外不要企图用 stream.str().resize(0),或 stream.str().clear() 来清除缓冲,使用它们似乎可以让stringstream的内存消耗不要增长得那么快,但仍然不能达到清除stringstream缓冲的效果(不信做个实验就知道了,内存的消耗还在缓慢的增长!)

    参考文章:

    https://blog.csdn.net/u014097230/article/details/52089530

  • 相关阅读:
    斐波那契数列 详解
    ASP.NET 系列:RBAC权限设计
    架构系列:ASP.NET 项目结构搭建
    EntityFramework系列:Repository模式与单元测试
    PHP 系列:PHP Web 开发基础
    Java Web系列:Spring Boot 基础
    ddddddd
    ddd
    asdfsf
    sdfsdfsdf
  • 原文地址:https://www.cnblogs.com/xiaoxue126/p/8969021.html
Copyright © 2020-2023  润新知