whats the difference between C strings and C++ strings?

前端 未结 3 912
执笔经年
执笔经年 2020-12-24 14:31

whats the difference between C Strings and C++ strings. Specially while doing dynamic memory allocation

3条回答
  •  感动是毒
    2020-12-24 15:14

    The difference is speed in some operations, and encapsulation of places where common errors occur. std::string maintains information about the content and length of the string. This means it is not prone to buffer overruns in the same way a const char * is. It will also be faster for most operations than a const char * because the length of the string is known. Most of the cstring operations call strlen() under the hood to find out the length of the string before operating on it, this will kill performance. std::string is compatible with STL algorithms and other containers.

    Lastly, std::string doesn't have the same "gotcha!" moments a char * will have:

提交回复
热议问题