C++ equivalent of StringBuffer/StringBuilder?

前端 未结 10 922
说谎
说谎 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:44

    You can use .append() for simply concatenating strings.

    std::string s = "string1";
    s.append("string2");
    

    I think you might even be able to do:

    std::string s = "string1";
    s += "string2";
    

    As for the formatting operations of C#'s StringBuilder, I believe snprintf (or sprintf if you want to risk writing buggy code ;-) ) into a character array and convert back to a string is about the only option.

提交回复
热议问题