Sign in to follow this  
frantic

#include / using std::

Recommended Posts

SiCrane    11839
You don't need to say using std::string. You can also do using namespace std or simply type out std::string every time you need to reference the string type. All the standard library types live in namespace std to avoid name collisions. That way you can define your own string class in the root namespace if you wanted to (though you probably would need a very good reason to do so).

Share this post


Link to post
Share on other sites
pi_equals_3    517
Quote:
Original post by frantic
#include <string>

This includes the string header file, which includes information about the string type.

Quote:

using std::string;

The string class, as well as the other standard functions and classes, are in the std namespace. The using statement moves the string class into the global namespace. Otherwise, every time you needed to use a string you'd have to type...

std::string str;

Share this post


Link to post
Share on other sites
njpaul    367
you need to say:
using namespace std;

The standard library resides in the std namespace. Read up on namespaces. You don't actually have to say 'using namespace std;' if you prefix every definition of string, as std::string, but this is a pain.

For example:


#include <string>

using namespace std;

void main()
{
string s = "astring";
}




and


#include <string>

void main()
{
std::string s = "astring";
}




are equivalent.

Edit: Beaten

Share this post


Link to post
Share on other sites
frantic    122
Thanks for the overwhelming reply.

I've read up on namespaces and how they can be used to group classes/functions together etc so if the std namespace already includes the string class in it then why do I also need to '#include <string>'

I'm sorry if im making no sense, this is just something I'm trying to get my head around. Simple solution I'm sure. Example, why does:

#include <string>

int main(){

string test = "test";
return 0;

}

not work even though I've included the string class? Whats difference between including 'using std::string' and not including it?

Share this post


Link to post
Share on other sites
Fruny    1658
If you look in the <string> header file, you will see something like:

namespace std
{
// Stuff

typedef basic_string<char, char_type<char>, allocator<char> > string;

// More stuff
}


Thus, the name string isn't part of the global namespace, but enclosed withing a namespace named std - so the fully-qualified name of the class is std::string.

In your program, you can directly refer to that class using the fully qualified naùe.

#include <string>

int main()
{
std::string test = "test";
return 0;
}


If you add using std::string;, you indicate to the compiler that when you mention string, you really mean std::string. It is a feature provided for convenience (and other reasons).

Share this post


Link to post
Share on other sites
MaulingMonkey    1728
Namespacing 101:

namespace foo {
class example {};
class example_2 {};
}

namespace bar {
class example {};
class example_2 {};
}

int main () {
example a;
/* example a: Do we mean foo::example or bar::example? This dosn't work to
* prevent us from having this type of problem if, for example, we later
* create another class of the same name in another namespace.
*/

foo::example b;
/* example b: It's explicit and clear we want to use the example found in
* the foo namespace. No matter how many new example classes we add into
* other namespaces, this will remain clear.
*/

using bar::example;
example c;
/* example c: We explicitly state we're using bar::example, so from this
* point onwards we'll know we're using bar::example and not foo::example
* when we just say "example".
*/

using namespace bar;
example_2 d;
/* example d: We can also explicitly say we're using an entire namespace.
* Same deal as with C, only it applies to all of the namespace's members.
*/

foo::example e;
/* example e: However, we can allways explicitly say we want foo's example,
* even after using statements for another namespace.
*/

}

Share this post


Link to post
Share on other sites
pi_equals_3    517
Quote:
Original post by frantic
I've read up on namespaces and how they can be used to group classes/functions together etc so if the std namespace already includes the string class in it then why do I also need to '#include <string>'


Yes, the string class is inside the std namespace, BUT the std namespace is defined inside the string header file.

Including <string> tells the compiler, "Hey, I'm going to use a bunch of stuff that's defined in this file!" The compiler sees that there is a namespace called 'std'. When you type 'using namespace std' you tell the compiler, "See all that stuff defined inside the std namespace? I want you to make it available globally so I don't have to type std:: all over the place."

Edit:

If you only included the file, the compiler would see that you used 'string' and it would think, "The only 'string' I see is in a namespace, so you must mean some other 'string' class."
Alternatively, if you only declared that you were using the std namespace, the compiler would think, "Ok, I'll treat objects in the std namespace as if they weren't in the namespace, but then it'll look for 'string' and it won't be able to find it since it's inside the header file.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this