| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Roger" |
| Date: |
16 Aug 2007 05:22:24 PM |
| Object: |
using std::cout |
Hello, I'm pretty new to C++ programming, and I'm teaching myself the
language using various sources.
This sounds stupid, but I am really confused on this... I was
wondering why we have to write a C++ program with this:
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "hello, world" << endl;
return 0;
}
or like this:
#include <iostream>
int main() {
std::cout << "hello, world" << std::endl;
return 0;
}
Why can't we just write it like this? :
#include <iostream>
int main() {
cout << "Hello, World << endl;
return 0;
}
My compiler, VC++ 2005 Express does not seem to compile the last chunk
of code. Why do we need std? I just need someone to clear up some
confusion. Hehe.
.
|
|
| User: "Robert Bauck Hamar" |
|
| Title: Re: using std::cout |
16 Aug 2007 05:34:49 PM |
|
|
Roger wrote:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the
language using various sources.
This sounds stupid, but I am really confused on this... I was
wondering why we have to write a C++ program with this:
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "hello, world" << endl;
return 0;
}
or like this:
#include <iostream>
int main() {
std::cout << "hello, world" << std::endl;
return 0;
}
Why can't we just write it like this? :
#include <iostream>
int main() {
cout << "Hello, World << endl;
return 0;
}
My compiler, VC++ 2005 Express does not seem to compile the last chunk
of code. Why do we need std? I just need someone to clear up some
confusion. Hehe.
std is a namespace. Namespaces exist to prevent names from accidentally
clash with each other. Imagine you work on a project with someone else, and
both you and your colleague define a function named 'doWhatIWant'. If now
you and your colleague define the name in different namespaces, this would
not be a problem. As the standard library contains a lot of names, it
defines most of them in the namespace std.
A note: I think
#include <iostream>
int main() {
std::cout << "Hello, World\n";
return 0;
}
is better. std::endl just sends a newline character to the stream, and then
flushes it.
--
rbh
.
|
|
|
| User: "Roger" |
|
| Title: Re: using std::cout |
16 Aug 2007 06:08:03 PM |
|
|
On Aug 16, 3:34 pm, Robert Bauck Hamar <roberth+n...@ifi.uio.no>
wrote:
Roger wrote:
Hello, I'm pretty new to C++ programming, and I'm teaching myself the
language using various sources.
This sounds stupid, but I am really confused on this... I was
wondering why we have to write a C++ program with this:
#include <iostream>
using std::cout;
using std::endl;
int main() {
cout << "hello, world" << endl;
return 0;
}
or like this:
#include <iostream>
int main() {
std::cout << "hello, world" << std::endl;
return 0;
}
Why can't we just write it like this? :
#include <iostream>
int main() {
cout << "Hello, World << endl;
return 0;
}
My compiler, VC++ 2005 Express does not seem to compile the last chunk
of code. Why do we need std? I just need someone to clear up some
confusion. Hehe.
std is a namespace. Namespaces exist to prevent names from accidentally
clash with each other. Imagine you work on a project with someone else, and
both you and your colleague define a function named 'doWhatIWant'. If now
you and your colleague define the name in different namespaces, this would
not be a problem. As the standard library contains a lot of names, it
defines most of them in the namespace std.
A note: I think
#include <iostream>
int main() {
std::cout << "Hello, World\n";
return 0;
}
is better. std::endl just sends a newline character to the stream, and then
flushes it.
--
rbh
So by defining it after the #include <iostream>, I won't have to use
std::cout?
.
|
|
|
| User: "" |
|
| Title: Re: using std::cout |
16 Aug 2007 07:28:27 PM |
|
|
So by defining it after the #include <iostream>, I won't have to use
std::cout?
Yes.
You could also, write "using namespace std", to register the complete
namespace.
#include <iostream>
using namespace std;
int main() {
cout << "hello, world" << endl;
return 0;
}
.
|
|
|
| User: "Roger" |
|
| Title: Re: using std::cout |
16 Aug 2007 07:41:20 PM |
|
|
On Aug 16, 5:28 pm, wrote:
So by defining it after the #include <iostream>, I won't have to use
std::cout?
Yes.
You could also, write "using namespace std", to register the complete
namespace.
#include <iostream>
using namespace std;
int main() {
cout << "hello, world" << endl;
return 0;
}
Wow! Thanks for the tip. I didn't know that. :-)
.
|
|
|
| User: "red floyd" |
|
| Title: Re: using std::cout |
16 Aug 2007 08:27:02 PM |
|
|
Roger wrote:
On Aug 16, 5:28 pm, wrote:
So by defining it after the #include <iostream>, I won't have to use
std::cout?
Yes.
You could also, write "using namespace std", to register the complete
namespace.
Wow! Thanks for the tip. I didn't know that. :-)
However, a good piece of advice, if you're going to do that is to never,
NEVER, *NEVER* place "using namespace std" in a header file. It can
mess up any other headers that follow yours.
If you feel like using "using namespace std;", place it in your .cpp
source file, after all includes.
.
|
|
|
| User: "Frank Birbacher" |
|
| Title: Re: using std::cout |
17 Aug 2007 04:40:22 AM |
|
|
Hi!
red floyd schrieb:
However, a good piece of advice, if you're going to do that is to never,
NEVER, *NEVER* place "using namespace std" in a header file. It can
mess up any other headers that follow yours.
:D Yes! A friend of mine recently got bitten by it. It made
std::ifstream and boost::filesystem::ifstream mix up.
If you feel like using "using namespace std;", place it in your .cpp
source file, after all includes.
Yes! *AFTER ALL INCLUDES*
Frank
.
|
|
|
|
| User: "Juha Nieminen" |
|
| Title: Re: using std::cout |
17 Aug 2007 04:28:27 AM |
|
|
red floyd wrote:
However, a good piece of advice, if you're going to do that is to never,
NEVER, *NEVER* place "using namespace std" in a header file.
My advice is to never use "using namespace" at all. In fact,
personally I never use the "using" keyword at all to circumvent namespaces.
For whatever reason the intuition of the beginner (and often the
not-so-beginner) programmer is that everything should be written as
shortly as possible, minimizing the amount of typing. The less you have
to type, the better. This is a very misleading intuition. In the long
run it backfires in the form of hard-to-read obfuscated code which is
difficult to understand and modify.
Personally I hate nothing more than having to read code written by
someone else who carelessly uses "using namespace std;" everywhere to
get rid of the std namespace. Then I encounter obscure function calls
like for example "fill(a, b, c);" and I can only wonder where that
function is defined. If it had been written as "std::fill(a, b, c);" it
would have been immediately obvious that it's a standard library function.
.
|
|
|
| User: "Frank Birbacher" |
|
| Title: Re: using std::cout |
17 Aug 2007 04:55:19 AM |
|
|
Hi!
Juha Nieminen schrieb:
Then I encounter obscure function calls
like for example "fill(a, b, c);" and I can only wonder where that
function is defined. If it had been written as "std::fill(a, b, c);" it
would have been immediately obvious that it's a standard library function.
I recently started to put "using namespace ...;" lines into the function
where I need them. I dislike the "std::" littering in a function which
is all about <iterator> and <algorithm>, i.e.
using namespace std;
transform(
istream_iterator<string>(file),
istream_iterator<string>(),
back_inserter(sequence),
&convertToMyData
);
Or whatever. This, however, should bring "using namespace std;" close
enough to the function calls in order to immediately draw attention when
reading the code.
Frank
.
|
|
|
|
|
|
|
|
|
|

|
Related Articles |
|
|