std::cout and using namespace std?

What's the difference between std::cout and using namespace std?
is std::cout a difference programming?
Nope. Using the using namespace std; is actually making programming easier. I could explain what a namespace is, but that would require you to have some more knowledge about C++ in order to understand. What you need to remember, is that using namespace std; makes your programmer life easier. It's exactly the same as typing:
1
2
3
4
std::cout
std::cin
std::endl
etc.


Hope I could help,
~ Raul ~
Last edited on
Thanks i finally understand it now :)
Well, I don't really get it why some people keep saying that std:: is good.
Then why did programers create namespaces ? For their fun ? Probably not.
They were created in order to simplify one's code. I, for myself, very often define my own namespaces (in most of my programs).
So, it's up to everyone to decide whether to use it or not, but that's my point of view.

~ Raul ~
Last edited on
They were created in order to simplify one's code.

I'm pretty sure they were created to prevent name collisions. For instance you can have two variables (or functions) with the same name if they're in different namespaces.

Go ahead, add using namespace std; to this:
1
2
3
4
5
6
7
8
#include <algorithm>

int count = 999;

int main()
{
	count = std::min(999, 1000);
}


The only thing I can say about it is: Easy!

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> // because there is no need of using <algorithm>

using namespace std;

int _count = 777;

int main(int argc, char *argv[]){
     _count = min(777, 888);
     cout << _count << "\n";
     system("pause");
     return 0;
}
777
Press any key to continue...


That's why C++ is a case sensitive programming language. So that you can do what I did, or this -> int Count = 999 //rest of the code , or anything else related to renaming the variable.
Example: int countNumberOfOccurrences = 999
That's how I write most of my programs. Explicitly write the variable name so that its usage is obvious, despite of your code, which if it would be part of a larger project would be considered (by any programer) as an ambiguous declaration (considering that you had other counters too).

EDIT: As I mentioned before, I was/am/will be using namespaces.

PS: Just ran your code with Visual C++ 2010:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using namespace std;

int count = 777;

int main(int argc, char *argv[]){
	count = min(777, 888);
        cout << count << "\n";
        system("pause");
        return 0;
}
777
Press any key to continue...


~ Raul ~
Last edited on
> because there is no need of using <algorithm>

#include <algorithm> is required for useing std::min() in a portable manner.

It is possible that on a particular implementation, #include <iostream> has gratuitously included a header which has std::min()


> int _count = 777;

Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace. - IS



> Just ran your code with Visual C++ 2010:

You don't have #include <algorithm>
Doesn't have #include <cstdlib> either.

You're missing the point, jumper007.
Whenever you write using namespace x;, you're making namespace x disappear.
You're not "using" that namespace, you're throwing it away. Then you have to worry about name collisions.
Last edited on
@JLB Nope. My code ran perfectly under Visual C++ 2010 without <algorithm> or <cstdlib>.
Yes, underscored variable names are used for namespaces. As I already said, I declare namespaces in most of my big projects, so I am used to declare them like that.
@Catfish I got the point. The problem is that you didn't. I got used to declaring/using namespaces. I won't change my programming style just for the sake of avoiding name collisions. As I define my own namespaces, I can give variables different names. It's easy for me to avoid that. Sorry if sounds bad, but that's how I like doing things.

PS: Message sent from my tablet pc, so excuse the possible mistakes.

~ Raul ~
Nope. My code ran perfectly under Visual C++ 2010 without <algorithm> or <cstdlib>.


He didn't say it wouldn't. What he said is that reliance on it working is not portable. Indeed, that same code you say compiled in VC++10, won't compile in VC++11.



closed account (zb0S216C)
TananTanTananTanToink wrote:
"What's the difference between std::cout and using namespace std?
is std::cout a difference programming?
"

Qualifying an identifier with its scope is more explicit than "using namespace X" -- at least we know where the identifier came from. In addition, qualifying an identifier on a name-space level actually assists the compiler with symbol look-up. If one is "using namespace std" and "using namespace Other", the compiler would have to look through both name-spaces to find the symbol. By qualifying the identifier with the name-space's identifier, the compiler knows exactly where to look.

jumper007 wrote:
"Yes, underscored variable names are used for namespaces."
Standard wrote:
"17.4.3.2.1 Global names [lib.global.names]

--Each name that contains a double underscore (_ _) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.

--Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.165

165) Such names are also reserved in namespace ::std (17.4.3.1).
"

Also, if you're going to be "using namespace" all the time, then there's no point in having one.

Wazzak
Last edited on
I personally prefer to avoid using namespace as well. Helps with clarity.
I prefer to use it. Always avoid it in header files, though.
Topic archived. No new replies allowed.