Thread: Namespace std

  1. #1

    Namespace std

    Is it better to use

    using namespace std;
    using std::cout;
    using std::cin;
    or maybe
    std::cout << "What should I use?\n";
    std::cin >> the.answer;
    Which of the following is considered 'better coding'?

    or should I possibly learn to use 'printf'?

    thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2001
    I recommend the second example.


  3. #3
    Registered User
    Join Date
    Jan 2003
    It all depends on how usefull you will find naming something "cout". The trade-offs are.

    using namespace std;

    I can't use vector, less, set, swap, greater, max, min, or count as names for anything.

    using std::cout;

    you can use any name except cout, however you cannot cut-and-paste a block of code that contains an unqualified cout. This sort of using is generally fine anywhere except headers, if your headers need any using's you should strongly consider wrapping them in your own namespace (feel free to polute the hell out of your own namespace, typedef int i; if you want)

    std::cout << "x = " << std::setw(5) << std::precision(4) << x << std::end;

    is ugly, but always safe.

  4. #4
    Registered User
    Join Date
    Dec 2002
    It depends on what you're doing. The only time I ever "use" an entire namespace ("using namespace std") is for quick testing programs, because I don't think it's a good idea. As you probably guessed from experience with cout, it allows you to call all the standard functions (and access objects) without qualifying them. This is bad if, for example, you want to create a function called "abs" but didn't realize that there's already a standard one by that name.

    I usually use the second method as well, because I know enough that if I'm "using" a std object not to create my own with the same name. I prefer to use the third method if I'm not going to be using a lot of something, i.e. just one or two calls to cout, but that's because I'm paranoid.

    Although it can't hurt to know, printf is rarely (if ever) used (in my own experience, anyway). Streams are usually more convenient and more powerful.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    For any major programming project, I'd definitely use one of the latter two. Personally, I hate writing 'std::cout' every time I want to use cout, so I usually go with the other two.

    Some libraries (unlike the STL) are nice however, in that within a namespace, they embed other namespaces making it easier to include only a set of logically related quantifiers (though this is more of a library design issue).
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 10:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM