Why do you prefer char* instead of string, in C++?

后端 未结 12 1179
北海茫月
北海茫月 2021-02-01 08:31

I\'m a C programmer trying to write c++ code. I heard string in C++ was better than char* in terms of security, performance, etc, however sometimes it

12条回答
  •  一整个雨季
    2021-02-01 08:55

    std::string is almost always preferred. Even for speed, it uses small array on the stack before dynamically allocating more for larger strings.

    However, char* pointers are still needed in many situations for writing strings/data into a raw buffer (e.g. network I/O), which can't be done with std::string.

提交回复
热议问题