How do I include the string header?
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).
You want to include <string>
and use std::string
:
#include <string>
#include <iostream>
int main()
{
std::string s = "a string";
std::cout << s << std::endl;
}
But what you really need to do is get an introductory level book. You aren't going to learn properly any other way, certainly not scrapping for information online.