Hello all!

I am working through a book here and it says that there is a class-template "basic_string" that is used in the namespace std as typedef basic_string<char> string; to create an alias for the "string" type.

However, I know that there is a header file <string> for the declaration of the string class... so what's the difference between the two?

And when should I rely on namespace std to define string as an alias vs importing the <string> file?

Thanks to all in advance!

Recommended Answers

All 6 Replies

string is defined in <string> and the defenition of string is typedef basic_string<char> string;. string is just a typdef and there is no actual string class in the standard. Whenever you want to use a std::string then you need to #include <string>

So in other words... if I want to use string methods, I #include <string> right? Just checking.

Define by what you mean with:

if I want to use string methods, I #include <string> right?

If you want to use anything release to a std::string or std::wstring then you need <string>. If you want to use things like strcmp(), strncat() and other from the C library they are in <cstring> and/or <string.h>

I mean methods like str.compare(), str.size(), str.max_size(), etc....

Two definitions string in formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end.
string use methods, #include <string> right.

@nathan.pavlovsky All member functions of std::string reside in the <string> header. Here is a good reference that shows you everything in <string>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.