# #include / using std::

This topic is 4853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

When I want to use strings in my C++ program why do I need to #include <string> and also say... using std::string;

##### Share on other sites
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 on other sites
Quote:
 Original post by frantic#include

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 on other sites
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 on other sites

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 on other sites
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 on other sites
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 on other sites
Quote:
 Original post by franticI'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 '

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.

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633332
• Total Posts
3011400
• ### Who's Online (See full list)

There are no registered users currently online

×