Can someone advise me on using std?

instead of "using namespace std"

Page 1 of 1

3 Replies - 680 Views - Last Post: 09 March 2009 - 12:05 PM Rate Topic: -----

#1 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Can someone advise me on using std?

Posted 09 March 2009 - 10:58 AM

Could someone please tell me what the pros and cons are of using

using namespace std;



and not using it.

I do not really know how i cant use it (if you know what i mean) although i have done a few tests and relised that without using namespace std i must write.

#include <iostream>

int main()
{

std::cout << "Hello world"<<std:: endl;

return 0;
}



basically i just want to know if i should use "using namespace std" and if i shouldnt, where should i implement "std::" and it would really help me if someone could explain why i should do this. I heard that "using namespace std;" "pollutes" your code.........

greatly appreciated. thank you.

This post has been edited by Ryan2: 09 March 2009 - 10:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can someone advise me on using std?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Can someone advise me on using std?

Posted 09 March 2009 - 11:05 AM

If its a small program and something relatively simple, then there really isn't any harm is importing the entire std namespace. It also saves typing. however, the problem arises when you have a larger program with numerous namespaces, perhaps a few you've made yourself, etc... you don't want to pollute the namespace by importing the entire std one. In that case you can import just what you want to use:

using std::cout;
using std::endl;
//etc...


Was This Post Helpful? 0
  • +
  • -

#3 Ryan2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 06-March 09

Re: Can someone advise me on using std?

Posted 09 March 2009 - 11:21 AM

View PostKYA, on 9 Mar, 2009 - 10:05 AM, said:

If its a small program and something relatively simple, then there really isn't any harm is importing the entire std namespace. It also saves typing. however, the problem arises when you have a larger program with numerous namespaces, perhaps a few you've made yourself, etc... you don't want to pollute the namespace by importing the entire std one. In that case you can import just what you want to use:

using std::cout;
using std::endl;
//etc...



I see, i didnt think of it like that. That makes sense, thanks alot.
Was This Post Helpful? 0
  • +
  • -

#4 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: Can someone advise me on using std?

Posted 09 March 2009 - 12:05 PM

using namespace xxxx;
will negate the purpose of namespaces especially in bigger projects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1