I am taking a programming class in school and I wanted to start doing some c++ programming out of class. My school using Microsoft Visual C++ 6.0 (which is from 1998) so it still uses <iostream.h>
rather than <iostream>
and using namespace std
. When I started working, I couldn't figure out how and when to use using namespace std
and when to just use things like std::cout<<"Hello World!"<<'\n';
(for example) as well as it's limits and other uses for the namespace keyword. In particular, if I want to make a program with iostream and iomanip, do I have to state "using namespace std" twice, or is there something different that I would have to use as well, or can I just do the same thing as I did with iostream? I tried googling it but I didn't really understand anything. Thanks in advance for the help.
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
If you use the header names without the
.h
, then the stuff declared/defined in it will be in thestd
namespace. You only have to useusing namespace std;
once in the scope where you want stuff imported in order to get everything; more than oneusing namespace std;
doesn't help anything.I'd recommend against
using namespace std;
in general, though. I prefer to say, for example,using std::cout;
instead, in order to keep names instd
from conflicting with mine.For example:
may cause mysterious issues, because
left
andright
exist in thestd
namespace (as IO manipulators), and they get imported if you lazily sayusing namespace std;
. If you meant to actually use the IO manipulators rather than output the variables, you may be a bit disappointed. But the intent isn't obvious either way. Maybe you just forgot you have ints namedleft
andright
.Instead, if you say
or
everything works as expected. Plus, you get to see what you're actually using (which, in this case, includes nothing from
<iomanip>
), so it's easier to keep your includes trimmed down to just what you need.Here is a good link that describes namespaces and how they work.
Both methods are correct, that is, you can either introduce a namespace with the "using" statement or you can qualify all the members of the namespace. Its a matter of coding style. I prefer qualifying with namespaces because it makes it clear to the reader in which namespace the function / class is defined.
Also, you do not have to introduce a namespace twice if you are including multiple files. One using statement is enough.
Specific to
using namespace std
You really shouldn't ever use it in a header file. By doing so, you've imported the entire 'std' into the global namespace for anyone who includes your header file, or for anyone else that includes a file that includes your file.
Using it inside a .cpp file, that's personal preference. I typically do not import the entire std into the global namespace, but there doesn't appear to be any harm in doing it yourself, to save a bit of typing.
Ok, handful of things there, but it is manageable.
First off, the difference between:
And using
Is simply a matter of scope. Scope is just a fancy way of saying how the compiler recognizes names of variables and functions, among other things. A namespace does nothing more than add an extra layer of scope onto all variables within that namespace. When you type
using namespace std
, you are taking everything inside of the namespacestd
and moving it to the global scope, so that you can use the shortercout
instead of the more fully-qualifiedstd::cout
.One thing to understand about namespaces is that they stretch across files. Both
<iostream>
and<iomanip>
use the namespacestd
. Therefore, if you include both, then the declaration ofusing namespace std
will operate on both files, and all symbols in both files will be moved to the global scope of your program (or a function's scope, if you used it inside a function).There are going to be people who tell you "don't use
using namespace std
!!!!", but they rarely tell you why. Lets say that I have the following program, where all I am trying to do is define two integers and print them out:When I use
using namespace std
, I am opening the door for naming collisions. If I (by random chance), have named a variable to be the same thing as what was defined in a header, then your program will break, and you will have a tough time figuring out why.I can write the same program as before (but get it to work) by not using the statement
using namespace std
:Hopefully this has clarified a few things.
Good question, Ryan. What
using namespace
does is importing all symbols from a given namespace (scope) into the scope where it was used. For example, you can do the following:In the above examples, all symbols in namespace
A
become visible in namespaceB
, like they were declared there.This import has affect only for a given translation unit. So, for example, when in your implementation file (i.e.
.cpp
) you dousing namespace std;
, you basically import all symbols fromstd
namespace into a global scope.You can also import certain symbols rather than everything, for example:
You can do that in global scope, namespace scope or function scope, like this:
It is up to a programmer to decide when to use fully qualified names and when to use
using
keyword. Usually, it is a very bad taste to putusing
into a header files. Professional C++ programmers almost never do that, unless that is necessary to work around some issue or they are 100% sure it will not mess up type resolution for whoever use that header.Inside the source file, however (nobody includes source files), it is OK to do any sort of using statements as long as there are no conflicting names in different namespaces. It is only a matter of taste. For example, if there are tons of symbols from different namespaces being used all over the code, I'd prefer at least some hints as for where they are actully declared. But everyone is familiar with STL, so
using namespace std;
should never do any harm.There also could be some long namespaces, and namespace aliasing comes handy in those cases. For example, there is a Boost.Filesystem library that puts all of its symbols in
boost::filesystem
namespace. Using that namespace would be too much, so people usually do something like this:Also, it is almost OK to use namespace aliasing in headers, like this:
.. and benefit from less typing. What happens is that users that will use this header, will not import the whole filesystem library by saying
using namespace MyLib;
. But then, they will import "fs" namespace from your library that could conflict with something else. So it is better not to do it, but if you want it too badly, it is better than sayingusing namespace boost::filesystem
there.So getting back to your question. If you write a library using C++ I/O streams, it is better not to have any
using
statements in headers, and I'd go withusing namespace std;
in everycpp
file. For example:somefile.hpp:
somefile.cpp: