Thread: What is "using namespace std; "

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    22

    What is "using namespace std; "

    I keep seeing people using this line in their code... I've never seen it til this forum... what does it do???
    <^>( * ; * )<^>

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Cool Standard C++

    That's required in standard C++.

    Pre-standard C++ your codes :

    #include <iostream.h>

    int main()
    {
    cout<<"Samples";

    return 0;
    }

    but in standard C++ this is equivalent:

    #include <iostream> // without extension .h
    using namespace std; // for shorthand cout

    int main()
    {
    cout<<"Samples";

    // without line "using namespace std;", you must type this
    // "std::cout<<"Samples";

    return 0;
    }

    That's it !

  3. #3
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Different libraries may contain identifiers of the same name but different scope. A namespace is used to define the scope of where identifiers and variables are placed. This feature helps prevent conflicts between different identifiers with the same name
    Code:
    #include <iostream.h>
    #include<string.h>
    You could use the namespace version:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    // or call the individual functions needed
    using std::cout;
    using std::endl;
    using std::strcpy;
    // obviously this is more specific - some programmers prefer this rather
    // than the first method which would include the entire contents of 
    // namespace.  Another option is just to preceed all library function calls
    // with the scope identifier std::
    This is simply used to provide some sort of standard and help prevent possible compile errors if identifier overlapping occurs. The old method is still acceptable. Schools and many businesses, however, have made the change or are beginning to look in that direction.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    Another method you can use to avoid bringing in the whole namespace but not have to write std::xxx all the time is:
    Code:
    include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;  // etc, for whatever parts you use
    
    int main()
    {
       int x;
       cout << "Enter x: " << endl;
       cin >> x;
       .
       .
       .
    }

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