How do I include the string header?

后端 未结 9 1132
旧时难觅i
旧时难觅i 2020-12-05 10:01

I\'m trying to learn about strings, but different sources tell my to include different headers.

Some say to use , but other

相关标签:
9条回答
  • 2020-12-05 10:09

    Sources telling you to use apstring.h are materials for the Advanced Placement course in computer science. It describes a string class that you'll use through the course, and some of the exam questions may refer to it and expect you to be moderately familiar with it. Unless you're enrolled in that class or studying to take that exam, ignore those sources.

    Sources telling you to use string.h are either not really talking about C++, or are severely outdated. You should probably ignore them, too. That header is for the C functions for manipulating null-terminated arrays of characters, also known as C-style strings.

    In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string, and it's in the std namespace, so its full name is std::string. You can avoid having to write the namespace portion of that name all the time by following the example of lots of introductory texts and saying using namespace std at the top of the C++ source files (but generally not at the top of any header files you might write).

    0 讨论(0)
  • 2020-12-05 10:11

    "apstring" is not standard C++, in C++, you'd want to #include the <string> header.

    0 讨论(0)
  • 2020-12-05 10:28

    You shouldn't be using string.h if you're coding in C++. Strings in C++ are of the std::string variety which is a lot easier to use than then old C-style "strings". Use:

    #include <string>
    

    to get the correct information and something std::string s to declare one. The many wonderful ways you can use std::string can be seen here.

    If you have a look at the large number of questions on Stack Overflow regarding the use of C strings, you'll see why you should avoid them where possible :-)

    0 讨论(0)
提交回复
热议问题