CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2000
    Posts
    204

    which is better to use STD lib.?

    there are two methods to use STD lib.

    1.using namespace std;----- use once
    2.std::string s;-------------- use it everywhere

    which is better?
    First know what you don't know, then you will know what you will know

  2. #2
    Join Date
    Jan 2001
    Posts
    588
    It's really hard to say which is "better" in this case. It depends. If you have a lot of instances where "std::string" would occur in your code, you may want to bring in the entire namespace. Or, if you use other objects from the namespace, like cout, cin, std::vector, std::list, or other standard library members, you might want to bring in the namespace. Alternatively, if you just use the std::string class, you could use this:

    Code:
    using std::string;
    This will allow you to omit the std:: qualifier from the class name when referring to strings. You might want to do this if you want just the string class, or if you're worried that you might have classes or objects with identical names than some standard ones that may conflict if you bring in the whole namespace.

  3. #3
    Join Date
    Sep 2002
    Posts
    1,747
    In addition to Bob Davis' great points, it also common to not want to bring the entire namespace std into a header file, and many get rather squeamish at bringing any namespace members into headers as well. Since lots of client translation units may eventually include your header, it is usually considerate to not pollute those translation units beyond their permission. So your headers should probably use fully qualified namespace names without usings. Your cpp files are basically personal preference, as they are your translation units, you have full namespace control, and you need not worry about client code polllution.
    */*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    "It's hard to believe in something you don't understand." -- the sidhi X-files episode

    galathaea: prankster, fablist, magician, liar

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: which is better to use STD lib.?

    Originally posted by liuty
    there are two methods to use STD lib.

    1.using namespace std;----- use once
    2.std::string s;-------------- use it everywhere

    which is better?
    Well...actually there are four...
    Code:
    // Using the full member name, including the namespace it belongs to:
    std::cout << "Hello World";
    
    
    // By taking advantage of Using-Declarations:
    using std::cout;                             // This declares cout in the current
                                                 // scope as synonym for std::cout
    cout << "Hello World";
    
    
    // By taking advantage of Using-Directives:
    using namespace std;                         // Which specifies that the current
                                                 // scope can refer to names in the
                                                 // 'std' namespace without using
                                                 // full qualifiers. This is mostly
                                                 // used when porting legacy code.
    cout << "Hello World";
    
    
    // Using aliases:
    namespace X
    {
      namespace Y
      {
        class Z { ... };
      }
    }
    
    X::Y::Z                                      // The full qualifier for 'Z' is
                                                 // 'X::Y::Z'
    
    namespace w = X::Y;                          // This declares 'w' as an alias for
                                                 // namespace 'X::Y'
    
    w::Z                                         // Access 'Z' using 'w::Z'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured