Directly write into char* buffer of std::string

后端 未结 3 884
失恋的感觉
失恋的感觉 2021-02-05 23:28

So I have an std::string and have a function which takes char* and writes into it. Since std::string::c_str() and std::string::data(

3条回答
  •  心在旅途
    2021-02-06 00:00

    I don't know what you intend to do with that string, but if
    all you need is a buffer of chars which frees its own memory automatically,
    then I usually use vector or vector or whatever type
    of buffer you need.

    With v being the vector, it's guaranteed that &v[0] points to
    a sequential memory which you can use as a buffer.

提交回复
热议问题