using std::cout

R

Roger

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.
 
R

Robert Bauck Hamar

Roger said:
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.
 
R

Roger

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.

So by defining it after the #include <iostream>, I won't have to use
std::cout?
 
K

kamistrike

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;
}
 
R

Roger

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. :)
 
R

red floyd

Roger said:
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.
 
J

Juha Nieminen

red said:
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.
 
F

Frank Birbacher

Hi!

red said:
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
 
F

Frank Birbacher

Hi!

Juha said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top