std::string vs. char*

前端 未结 3 513
滥情空心
滥情空心 2021-02-04 07:56

does std::string store data differently than a char* on either stack or heap or is it just derived from char* into a class?

3条回答
  •  走了就别回头了
    2021-02-04 08:34

    These solve different problems. char* (or char const*) points to a C style string which isn't necessarily owned by the one storing the char* pointer. In C, because of the lack of a string type, necessarily you often use char* as "the string type".

    std::string owns the string data it points to. So if you need to store a string somewhere in your class, chances are good you want to use std::string or your librarie's string class instead of char*.

    On contiguity of the storage of std::string, other people already answered.

提交回复
热议问题