Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using namespace std

Status
Not open for further replies.

H3RO

Programmer
Jan 8, 2003
12
0
0
LT
Hi. What's the features for &quot;using namespace std;&quot; in VC ? Thing it should be used whenever including <fstream> , but I really dunno...What does it do? What it's responsible for?
 
Rather than specifying &quot;using namespace std;&quot; you may be better off specifying &quot;std::vector&quot;, &quot;std::cout&quot;, &quot;std::string&quot;, etc.

This whole subject has been a cause for argument amongst programmers for a while. Using the latter involves more typing but avoids ambiguities and confusion in the code.

You should use std with any of the STL containers, algorithms, etc.
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
C++ hasn't always had namespaces; not named ones, anyway. There used to just be global stuff. i.e. all the stuff was scattered out on the floor for anyone to use. Messy, messy.

Then, they decided to let programmers define namespaces. This lets us take all our stuff, clean it off the floor, and put it into nice little packages. We can use what we want from each package without leaving everything else strewn all over the place. Standard C++'s stuff is all in one box, and it's called &quot;std.&quot; It has a bunch of cool stuff in it... vector, string, ostream, pair, map, binary_function, istreambuf_iterator... stuff you use every day.

Now, think of &quot;using namespace std;&quot; as taking that box and dumping it all out on the floor. You get all that stuff plus about 12,000 other things you've never even heard of. That's the way used to be. That makes it easier to work with older programs that expect to have everything on the floor, but that's really the only reason to use it (that and teaching newbies C++).

Statements like &quot;using std::vector&quot; are the more civilized, more correct way of doing things. Think of them as taking a specific item out of the box and using it. It's more explicit, it's neater, it enforces the &quot;least access&quot; ideal, and it's just all around better practice.
 
Nice analogy chipper! ~cdogg

&quot;The secret to creativity is knowing how to hide your sources.&quot;
- A. Einstein
 
Instead of &quot;using namespace std;&quot;, you can also state &quot;using std::cout;&quot;, &quot;using std::vector;&quot;, etc.. That way you don't get the &quot;dumped box&quot; effect, and you still save some typing.

 
Wow that is great guys !!! You've all been of a big help. My lecturer haven't explained a thing on it and I'm curious as always why the heck is that routine for?!?! You guys are so smart and cool, god how I wish I could master C++ one day to know as many as most of you here. Now I understad where how those vectors and strings were used to work under std, that is just great gathering more and more knowledge with time not just from lecturers , but and from you guys. By the way, chipperMDW,...you're speach was just very clear, smart, interesting and intelligent....aren't you a lecturer yourself? You seem to rock ^_^
 
Well, I'm not a lecturer, but I'm a student and I work in a lab to explain stuff to beginning programmers, so I suppose I'm pretty close to one.

And thank you; you're right, I do rock. :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top