Why use c strings in c++?

后端 未结 18 2144
终归单人心
终归单人心 2021-02-07 04:12

Is there any good reason to use C-strings in C++ nowadays? My textbook uses them in examples at some points, and I really feel like it would be easier just to use a std::string

18条回答
  •  长情又很酷
    2021-02-07 04:37

    Let's say you have some string constants in your code, which is a pretty common need. It's better to define these as C strings than as C++ objects -- more lightweight, portable, etc. Now, if you're going to be passing these strings to various functions, it's nice if these functions accept a C string instead of requiring a C++ string object.

    Of course, if the strings are mutable, then it's much more convenient to use C++ string objects.

提交回复
热议问题