473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

cout vs std::cout

I'm getting back into C++ after a long hiatus (they didn't have
namespaces back then).

I know this question is completely subjective, but I'd be interested in
hearing which is
the "better" style and what the pros and cons are (I'm using cout as
example, but it really
applies to any similar construct):

1) using std::cout;
cout << "This is a test";

2) std::cout << "This is a test";

The difference being prefixing every cout with 'std' or declaring it
ahead of time. The second
method could be used if cout existed in more than one spot, but how
often is that an issue?

I've seen both methods in code, but I'm seeing #2 a lot more frequently.

Sep 28 '08
58 4762

just jumping in to thward my nose into wallstreet of c++ not to get
broken .

reasons are
1. using namespace std is not good as lot of people in this thread
aldready mentioned . why ?

assume you have 2 functions with same name may be fool() within
namespace std and fool() outside wihtout any namespace .
using namespace std
fool() --always triggers fool() inside namespace

During your code at later stage you want to call other fool() outside
of the namespace then also fool() inside std namespace gets
trigered . you should use extra ::fool() to overcome this problem why
allthose nasty blunders .

better use std::fool() wherevr u need . hope my nose not broken in c++
wall street. :)


Oct 6 '08 #51
microcassanova wrote:
reasons are
1. using namespace std is not good as lot of people in this thread
aldready mentioned . why ?
That's why you should think before you write any piece of code. I doubt
if creating function (or class or method or whatever) with the same name
as something in std namespace is a good idea. However, if you really need
to do this, then it is, in fact, better not to use "using namespace std"
everywhere.

In small programs it isn't a problem to help yourself and put using
namespace std on the top of the file. If you are working on a big
project, you have to think what you are doing and remember that:
std::cout << "blah";
is always safer than:
using namespace std;
....
cout << "blah";

Pawel Dziepak
Oct 6 '08 #52
Pawel Dziepak wrote:
microcassanova wrote:
>reasons are
1. using namespace std is not good as lot of people in this thread
aldready mentioned . why ?

That's why you should think before you write any piece of code. I doubt
if creating function (or class or method or whatever) with the same name
as something in std namespace is a good idea. [...]
Whoa. Why should you ban yourself from using identifiers like
'list', 'vector', 'sort', 'min'...
Pawel Dziepak
Schobi
Oct 6 '08 #53
Hendrik Schober wrote:
Whoa. Why should you ban yourself from using identifiers like
'list', 'vector', 'sort', 'min'...
Not to talk about the more obscure function names in the standard
library, many of which the programmer might have never heard of. Just
browse <algorithmor <functionalor any of the other standard headers
for many examples.
Oct 6 '08 #54
Hendrik Schober wrote:
Whoa. Why should you ban yourself from using identifiers like 'list',
'vector', 'sort', 'min'...
I didn't say that it is forbidden or something like that. I think that
just for your own good you should consider choosing another identifiers.
However if you like thinking if that "cout" stands for "std::cout" or
"myown::cout" than I see no reason why to "double" identifiers.
Everything depends on kind of project you are developing. For example, if
it uses only its own version of lists, vectors, etc. it's normal that you
don't create any "strange" names. However, if you sometimes use std::list
and sometimes myown::list, then, together with "using namespace x" you
can do something bad.
If code is easier to read and understand, there is smaller chance of
making stupid mistakes.

Pawel Dziepak
Oct 6 '08 #55
In article <gc**********@cb.generation-online.de>, sp******@gmx.de
says...

[ ... ]
As I recall, the editing to fix the code actually took less time than
the re-compilation...

:)

(Implementing a bunch of std lib containers so that they have
less errors than bought ones isn't something most of us finish
on a sunny Sunday afternoon.)
The quick part was changing the code so it used the containers in the
standard libary (after it was fixed) instead of my replacements. The
replacements themselves did take just a bit longer (though my
recollection is that implementing them wasn't quite as much work as I
expected either).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Oct 7 '08 #56
In article <pa*********************@quarnos.org>, pd******@quarnos.org
says...

[ ... ]
That's why you should think before you write any piece of code. I doubt
if creating function (or class or method or whatever) with the same name
as something in std namespace is a good idea. However, if you really need
to do this, then it is, in fact, better not to use "using namespace std"
everywhere.
One of the reasons for putting the standard library into a namespace is
that you don't (and in general _can't_) know all the names it might
define. As such, you might have duplication even if you avoid all the
names you know about.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Oct 7 '08 #57
On 7 Pa¼, 04:39, Jerry Coffin <jcof...@taeus.comwrote:
One of the reasons for putting the standard library into a namespace is
that you don't (and in general _can't_) know all the names it might
define. As such, you might have duplication even if you avoid all the
names you know about.
It won't be a problem. Not using the same names is a hint that can
make understanding the code easier. It's definitely not a rule.
Everybody write codes in a way they like the best and I would just
think for a moment before using the identifier I know that is used in
standard library. Namespaces solves almost all problems with
conflicting names, but overusing "using namespace x" make is a bit
pointless.

Pawel Dziepak
Oct 7 '08 #58
In article <gc**********@cb.generation-online.de>,
Hendrik Schober <sp******@gmx.dewrote:
>Yannick Tremblay wrote:
>In article <gc**********@cb.generation-online.de>,
Hendrik Schober <sp******@gmx.dewrote:
>>Yannick Tremblay wrote:

In this case, "String" might be acceptable but this a only the first
step on a slippery road. Be careful or the namespaces created by
worse programmers than yoursel will start ressembling hungarian
warts.

Don't you think that a bunch of programmers who decide
that they generally want to explicitly spell out every
namespace name for every identifier have a typing pain
threshold high enough to not to shorten names beyond
usability?
"Worse programmers than yourself". By virtue of intelligently
participating in a philosophical discussion about probgramming and by
virtue of participating in drawing up a code guideline document, I
would conclude that you are well above average as a developper.
Coding guidelines are arguably almost redundant for a good programmer
(apart from agreeing on minor stylistic standardisation like CamelCase
vs under_scored). Coding guidelines are at their most important for
below average programmers. So the peoples that agreed on the above
will most probably do the right thing and take a sensible decision on
naming. I have no problem with that. It the new graduate, I am
concerned about.

Slightly related issue: one of that my (many) issue with
(pseudo-)hungarian notation is that it tend to make identifiers more
cryptic. IME, the average lenght of identifiers stays about the same,
it's just that characters that would have been used for the
descriptive name are instead used for the (pseudo-)hungarian prefix.
Yan

Oct 8 '08 #59

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: qazmlp | last post by:
I hope comp.lang.c will not find the following question as a complete off-topic. I would like to remove ie.comment out the 'cout' statements during compilation(actually preprocessing) time. ...
6
by: Omid | last post by:
Hi. I have problems when I try to redirect everything that is sent to cout to a file. I have one piece of code that works and one that does not work. The only difference is which headers I use....
5
by: Mastupristi | last post by:
I want to obtain the c++ equivalent of: unsigned short us = 347; printf("0x%04hX",us); that outputs "0x015B" I ried with: cout.setf(ios_base::hex,ios_base::basefield);
10
by: Gurikar | last post by:
How to make cout not printing on the console. i mean void main() { cout<<"Hello world"<<endl; } Is there any way where i can block printing on console even when iam
7
by: Joe C | last post by:
I'd like to have better control of text output to the console. I'm using Windows and have a little (non-standard) function to help me out: #include <windows.h> void locate(int x, int y ) {...
16
by: mrDumbass | last post by:
#include <iostream> #include <algorithm> #include <string> #include <fstream> using namespace std; /* i would like to keep the numbers of the output : Power , step etc, on the same place...
13
by: Jim Langston | last post by:
I had asked this in comp.lang.c++ with out any answers that would actually work, so I'm hoping someone here may know a way. I am calling C library functions that want to output to stdout. I need...
1
by: William Chavula | last post by:
void basic() { cout<<"---FOLLOW THESE STEPS TO DIAGNOSE INTERNET PROBLEMS---\n\n"; cout<<"---STEP 1---\n\n"; cout<<"Run your mobile partner software\n\n"; cout<<"---STEP 2---\n\n";...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.