Hey guys, I just have a simple (?) question. What difference is there, if any, between cin and std::cin (for example)? If the latter is better form, why is it so?

Hey guys, I just have a simple (?) question. What difference is there, if any, between cin and std::cin (for example)? If the latter is better form, why is it so?

They are the same.
however you must bring the cin into scope by one of these ways:

using namespace std; //declare global scope (not preferred but OK for homework)
using std::cin; //declare only what is used in your program (prefered)

OR
std::cin >> helpme; //make yourself crazy and use the scoping operator everywhere in the program.

Unless you are at a professional level there are very few times where you shouldn't include

using namespace std;

...unless of course you want to learn something.

I disagree.

I'm not alone. You may also want to read here.

You shouldn't program to avoid learning things. The purpose of namespaces is to encapsulate your code into logical units and avoid name collisions. The using statement explicitly defeats that purpose.

So, the question should be: when is it appropriate to use using?

The answer is simple: don't contaminate other modules' code with foreign namespace data. For example, a using statement should not appear in any header file, because any program that #includes it is automatically contaminated with a (probably unwanted) namespace.

A good rule of thumb, then, is when writing library code either don't use using or only use it in local blocks; and when writing your main application (the file that has the main() function in it) feel free to use it, but beware.

For more, see this thread wherein Ancient Dragon and Narue make very good points on the use of namespaces.

Hope this helps.

using namespace std;
is required for u 2 be able to use cin only

Not so.
In my previous post I stated that you could also declare it globally with listings;

using std::cin;
using std::cout;

this will also eliminate the need to type the prefix std::

>using namespace std;
>is required for u 2 be able to use cin only
No, it's not. You have to qualify for the std namespace to use any standard name, but a using directive isn't the only way to do it. In fact, it's probably the worst way to do it because it's easier to get wrong. Most people just put it in the global namespace and flood the world with every name in their included headers.

It's nice how people reply to threads without reading them. :icon_rolleyes:

i was about to ask this question too

so what's the proper way?

i always do declare globally this one

using namespace std;

should i just declare this one globally instead (if i'm using it)

using std::cin;
using std::cout;

i was about to ask this question too

so what's the proper way?

i always do declare globally this one

using namespace std;

Didn't you even bother to read the rest of this thread ?

i did read.

what i'm asking exactly is when i declared globally:

using std::cin;
using std::cout;

the one's i'm using in statements would be

plain cin and cout only

or still

std::cout<<
std::cin>>


i was just reassuring ;)

well i got my answer

Personally, using should be use whenever you use those functions in the large amount of time. For example: using std::cout; and using std::cin are the most common function that you use. These functions should use using globally. For those functions which only use once in the code, should not be use using globally or not even locally. Anyway, since this is my opinion, I am looking forward to heard what other think about it.

To recap, here are the three options using best practices:

#include <iostream>

int main()
{
  using namespace std;

  cout<<"Hello, world!\n";
}
#include <iostream>

int main()
{
  using std::cout;

  cout<<"Hello, world!\n";
}
#include <iostream>

int main()
{
  std::cout<<"Hello, world!\n";
}

If you have a using directive or declaration in the global scope, you're probably writing unsafe code. My personal preference is to explicitly qualify my names with std:: rather than any form of using statement unless I'm converting legacy code.

For some functions, I think it should be declare globally due to many repeating use of it. In the below example, I spam the declaration in every function. Since these functions are needed to almost every function, why not declare it gloablly?

#include <iostream>

int function1()
{
  using std::cout;
  using std::cin;
  // your code here
}

int function2()
{
  using std::cout;
  using std::cin;
  // your code here
}

int main()
{
  using std::cout;
  using std::cin;
  // your code here
  return 0;
}

I think it's better to make it globally

#include <iostream>
using std::cout;
using std::cin;

int function1()
{
  // your code here
}

int function2()
{
  // your code here
}

int main()
{
  // your code here
  return 0;
}

>Since these functions are needed to almost every function, why not declare it gloablly?
For the same reason you shouldn't have a global using directive. I doubt anyone will create their own cin and cout, but I see min and max used a lot, yet both of those are standard names. Rather than pick and choose which to release globally and which not to, I just apply a blanket guideline and forget about it.

>I think it's better to make it globally
That's your opinion. Mine is different. No biggie.

Thanks a lot guys. I'm still pretty new to C++, so this really helped me out. So the line using namespace std; has nothing to do with using iostream over iostream.h? Guess that's another misconception cleared up.

This article has been dead for over six months. Start a new discussion instead.