using namespace std;

Hi, I want to know why we use this in all codes.

I know that in this namespace are some entities we use in our programs, but where I can find exactly who are them? There is any relationship to <iostream> ?

ty.
Last edited on
If you don't write using namespace std; you will have to explicitly add std:: before anything in the std namespace.

Example:
1
2
3
std::cout << "Hello World!" << std::endl;
std::string s;
std::vector<std::string> vs;


Some people, myself included, prefer to write std::.
Well, everything thats in the c++ standard library is in the namespace std. Here you can see them: http://www.cplusplus.com/reference/

<iostream> is one of those headers
Catfish, I understand how we use it.

But for example, if we use:
1
2
3
4
5
6
#include <iostream.h> // with ".h"
int main() {
cout << "Hello World !" << endl;

retun 0;
}


It will work, but g++ will say that it is deprecated, so I imagined that this type of programming is old. It looks like a structured programming.

Is in it a real diference ?

coder777, I'm learning OOP, and I'm thinking that when we use "using namespace std" we say to compiler that we will work in "OOP mode"?

ty.
closed account (1vRz3TCk)
co1ote wrote:
#include <iostream.h> // with ".h" will say that it is deprecated
that would be pre-standard code.

"using namespace std" Has nothing to do with OOP, it is just a way of sidestepping scope resolution.
By the way, if you only need a few distinct functions, you can tell it just that. For example, if you only plan on using cout and endl, there is no need to use the entire std namespace. Instead:
1
2
using std::cout;
using std::endl;
So its just a scope, and when I use it I'm saying that all entities that are in c++ standard library will be used.

std = http://www.cplusplus.com/reference/

But they will only be used if I include the specific header ?
Like <iostream> , <cstdlib>

ty.
It means that all entities in the namespace std will be "lifted" into the global namespace, meaning you won't have to use the std:: for them. If you don't type it, then you can still use them albeit with the prefix std:: so that the compiler knows where to look for them. That's all it really does here.

You can't avoid #including headers with it, I'm afraid. :/

-Albatross
Last edited on
Albatoss, you get the point, the thing is that its is strange including the header but only be able to use it after have declared the namespace std.

Maybe, there is a big diference between <iostream.h> and <iostream> .

Like the example I used above, where cout works without the namespace std.

ty all.
<iostream.h> was created before the standard dictated that all members of the standard library have to be in the std namespace. There's a difference. ;)

And as I and many people above me have said, you do not have to use using namespace std to use the members of that header. This:

1
2
3
4
5
#include <iostream>
int main() {
    std::cout << "...world? Why don't you ever respond?\n";
    return 0;
}


...is perfectly valid.

-Albatross
No, it's not weird. It's context. Just like the statement "I loved their latest album" is meaningless if you don't know who "they" is, the compiler has no way of knowing you meant std::cout, unless you specify it, either explicitly (std::cout, cfr "The Beatles' last album") or implicitly (using namespace std, cfr "What's your opinion on the Beatles?").

Your first sentence shows that you still don't get it. You CAN use anything from the headers you included. You just have to provide the context.
I google and found it:
http://www.physicsforums.com/showthread.php?t=487824

Additionally the 1990 C standard defined a number of header files with .h.
These header files have been incorporated into the 1998 C++ standard and they still work.
However, with the new thought that standard C++ header files should not have .h, the same files can also be included without .h, but they do need a "c" prefix then.
Moreover, all standard C++ functions and symbols reside in the namespace "std" now, although the old-style header files still reside in the global namespace.

For instance in C++ the following 2 code fragments are equivalent:

#include <stdio.h>
scanf("%d", &number);

#include <cstdio>
std::scanf("%d", &number);

Note that non-standard C++ header files are conventionally still written with .h, such as <windows.h> and any header file that you introduce yourself.


I think now I get the idea.

So, I think game over. ;]

ty all.
Topic archived. No new replies allowed.