Why should one use std::string over c-style strings in C++?

后端 未结 6 2021
慢半拍i
慢半拍i 2020-12-15 23:36

\"One should always use std::string over c-style strings(char *)\" is advice that comes up for almost every source code posted here. While the

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 00:16

    1. std::string manages its own memory, so you can copy, create, destroy them easily.
    2. You can't use your own buffer as a std::string.
    3. You need to pass a c string / buffer to something that expects to take ownership of the buffer - such as a 3rd party C library.

提交回复
热议问题