To std:: or not...that is the question

This is a discussion on To std:: or not...that is the question within the C++ Programming forums, part of the General Programming Boards category; Is it better to use std::cout and std::cin in my programs or can I jsut use cout. Is this dependant ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    To std:: or not...that is the question

    Is it better to use std::cout and std::cin in my programs or can I jsut use cout.

    Is this dependant on using namespace std;?

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
         cout << "Hello, world!" << endl;
         return 0;
    }
    ... and ...
    Code:
    #include <iostream>
    
    int main()
    {
         std::cout << "Hello, world!" << std::endl;
         return 0;
    }
    Are essentially the same thing.

  3. #3
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    As BMJ said it's more of a personal preference since they will behave ver similar. Sometimes i prefer to use for instance
    Code:
    #include <iostream>
    using std::clog;
    using std::endl;
    
    int main()
    {
            clog << "Logging feature" << endl;
            return 0;
    }
    The only reason i sometimes prefer to use this scheme is because i can later on change all logging calls simply by changing the namespace for some other namespace as long as it has clog also defined. so by changing:
    Code:
    using std::clog;
    to
    Code:
    using my::clog;
    I can override all calls to clog and change the logging features without interefering with std::endl or any other std:: definitions. This allows a per-file basis changes without having to go through the whole file changing the clog calls. This is obviously useful only sometimes and always as a personal preference.

    Hope this helps a tad, cheers.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    38
    Quote Originally Posted by BMJ
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
         cout << "Hello, world!" << endl;
         return 0;
    }
    seems to be easier, because you type less. But in the end, I think it's whatever you're comfortable with.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I prefer to put std:: on everything and never use a using directive. The reason the namespace was created was to avoid naming conflicts. If you are always putting in the using directive, then you lose the benefits of the namespace. In some cases it does matter, so I prefer to be consistent and use std:: everywhere. It also makes it clear that you are using a standard library name versus a hand implementation or something from another library.

    For beginner programs it rarely matters, but if you intend on becoming more than a beginning programmer consider avoiding the using directive and making the effort to type std::.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Is it better to use std::cout and std::cin in my programs or can I jsut use cout.

    Is this dependant on using namespace std;?
    Using just cout will only work if you have the 'using namespace std;' line before main(). It has to do with a topic called 'namespaces' which you will learn about later. It requires much less typing if you include 'using namespace std;' and just use 'cout', 'cin', and 'endl', so for now just continue doing that.
    Last edited by 7stud; 04-02-2006 at 09:48 PM.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    I would go with the process LinuxCoder suggested - it's flexible and there's probably less of a chance that unwanted object code (from header files) will end up in the final result.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,168
    There are times when you don't want to have a using directive in force (whether a using namespace std; or "using std::cout;" or similar). For example, one way to tie yourself (and others using your code) into future knots of ambiguity [code that won't compile due to there being multiple candidates for a name like cout] is to place a "using namespace std;" directive into a header file. So, if you want to use cout within a header file (eg within inline member functions of a class), then avoid the using directive and call the object std::cout.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    I rarely use the 'using namespace <>' method. When looking at my code I can certainly tell that vector, list, map, etc, etc. are in namespace std. Without using the form std::vector, std::list, etc, etc, it can get confusing later. Just a personal preference.

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    How about "/n" and endl?

    Which is best?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Do you mean '\n' and std::endl? Again, it doesn't really matter. Use \n when you don't need to explicitly flush the buffer (most of the time). Use endl when you do want to add a newline and flush the buffer. Since the buffer should be automatically flushed if you use cin to wait for input, or when the program closes, you don't need the endl explicitly in those cases.

    I break this rule by using endl a lot when '\n' would work. I'm more used to it and I think it is clearer, and like I said, it doesn't really matter.

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    I think my book called it Simple Polymorphism when you use the same function name with different parameters...do you need to write a different definition and code?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Default Constructor Question
    By NoviceC in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 02:12 AM
  3. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 02:10 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  5. using namespace std; question.
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21