Mae
Mae

Reputation: 3

is the string type a header file or in the standard namespace?

I'm a newbie to C++, and programming for the most part, just started learning a few days ago, and I'm a bit confused about this. Is the string variable type in the standard namespace? I found I can use strings without using #include <string>. I can also use using namespace std;, to activate the use of strings, or std::string mystring; for example.

I know that using using namespace std; allows for the use of all commands/functions within the standard namespace, ie cout.

If string is within the standard namespace, is #include <string> the same as saying using std::string; ?

Upvotes: 0

Views: 516

Answers (2)

MSalters
MSalters

Reputation: 179779

"I found I can use strings without using #include <string>"

That's coincidence. You included another header that by chance included <string>. This is allowed in C++, and pretty common for e.g. <sstream>. But you can't rely on this. If you need std::string, you should #include <string>.

This will include std::string, from the std namespace. This namespace is actually defined by multiple headers. Unlike classes, namespaces do not need to be defined in one header; they can be spread over multiple.

Upvotes: 0

eerorika
eerorika

Reputation: 238301

std::string is a class defined in the standard library header <string>. Like all names of the standard library, it is declared in the namespace std.

is #include <string> the same as saying using std::string; ?

No. Those have two entirely different meanings.

#include <string> means "include the content of the header <string> into this file". The header contains the definition of the class std::string among other things.

using std::string; essentially means "declare the identifier string as a type alias to std::string in the current namespace".

Upvotes: 1

Related Questions