-
What is using name std;?
I'm reading the tutorials in the site, but they didn't explain about the line
"using namespace std;", I write it but I have no idea what it does, can anyone explain?
I guess it's something like the #include <iostream> that includes some commands that I will not be able to use if I won't write it, but I'm not sure...
-
Every standard class and function is in the std namespace. You can think of a namespace as a big box with everything thrown into it. When you want to look at one of the names, you need to know what box to look in. "using namespace std;" tells the compiler to automatically look in the std namespace.
You can also tell the compiler to only look for one name with "using std::<name>;" where <name> would be something like cout or endl, or you can tell the compiler what namespace to look in with every use by prefixing the name with std::. The following program demonstrates the last two.
Code:
#include <iostream>
using std::cout;
int main()
{
cout<<"Hello world!"<<std::endl;
}
Notice how cout isn't prefixed because I already told the compiler to look for "std::cout" whenever it sees "cout". endl has to be prefixed because otherwise the compiler won't know that "endl" is actually "std::endl".
-
This line is bringing the std namespace into scope. Almost all of the functions / objects declared in iostream and various other header files are declared in the std namespace to avoid name clashing. Without doing that when you write cout the compiler doesn't now what you are refering to.
Here is a thread that discussed namespaces recently and I think it did a decen job. Let me know if you have further questions.
-
It declares that you will use cout and cin and vector and EveryStdYouCanThinkOf instead of std::Whatever.
The thing is, cout and cin are really std::cout and std::cin. This saves you from having to type up std::Whatever everytime you want to use it (which is sometimes several hundred times in larger programs.) It saves a lot of time and effort.
And now time for a story
Once upon my time I went through a rebellious stage in my coding. I didn't use using namespace std; and used gotos and global variables anywhere I could. I was a bad boy.
One day I wrote a RPG and when I compiled it there were about, um, 219 and a half errors (more or less). After that, um, incident I read everything I could about proper coding and my code was perfect for a month. After that I went back to coding normally.
THE END
EDIT: D'oh! I hate when I tell stories when there are no posts and when I finish remembering my learning-of-C++-hood there are two other posts. NO!!!!!
-
Hmm ok I think I understood it, I'm just not sure then why is there #include <iostream> and this too?
by what I understood the #include <iostream> includes all the commands and the using namespace std; tells the computer something about thier names... =] ok I'm confused
by the way fuh, nice story :P
Edit: I read the message that andyhunter gave me, and I think I understood,
sometimes it's easier to write the same names for variables so there are namespaces that in them you can write the same variable (in two diffrent namespaces), and you just need to write the name of the namespace before the variable so it will know which variable are you talking about... am I right?
-
-
They explained it all very well, but I would also like to point out that you can make your own namespaces.
namespace MyNamespace
{
int x;
}
int x; //a global variable
MyNamespace::x is not the same as global x. They have the same name, but their namespace is different, therefore they are different variables (although their name is the same).
Classes are also namespaces, that's why in two different classes you can have two variables with the same name, but they are different variables because they are in separate classes (different namespaces).
It's akin to having the same numerical value of a number, but the units may be different. Maybe not the best example but it sort of works.
-
Ok I understood, I was about to ask that isn't it pretty much like structures, but never mind I reminded in what structures are diffrent, so I think I understood all about it, thanks alot for all that helped =]
-
Also, <iostream.h> and <iostream> are two different things. <iostream> is included in the std namespace, which is why you have to include 'using namespace std' or specify the namespace before you use each iostream command.