C++ equivalent of StringBuffer/StringBuilder?

前端 未结 10 921
说谎
说谎 2020-11-29 15:51

Is there a C++ Standard Template Library class that provides efficient string concatenation functionality, similar to C#\'s StringBuilder or Java\'s StringBuffer?

10条回答
  •  鱼传尺愫
    2020-11-29 16:37

    The std::string.append function isn't a good option because it doesn't accept many forms of data. A more useful alternative is to use std::stringstream; like so:

    #include 
    // ...
    
    std::stringstream ss;
    
    //put arbitrary formatted data into the stream
    ss << 4.5 << ", " << 4 << " whatever";
    
    //convert the stream buffer into a string
    std::string str = ss.str();
    

提交回复
热议问题