What is using name std;?

This is a discussion on What is using name std;? within the C++ Programming forums, part of the General Programming Boards category; I'm reading the tutorials in the site, but they didn't explain about the line "using namespace std;", I write it ...

  1. #1
    LBY
    LBY is offline
    Registered User
    Join Date
    Nov 2004
    Posts
    12

    What is using name std;?

    I'm reading the tutorials in the site, but they didn't explain about the line
    "using namespace std;", I write it but I have no idea what it does, can anyone explain?
    I guess it's something like the #include <iostream> that includes some commands that I will not be able to use if I won't write it, but I'm not sure...

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Every standard class and function is in the std namespace. You can think of a namespace as a big box with everything thrown into it. When you want to look at one of the names, you need to know what box to look in. "using namespace std;" tells the compiler to automatically look in the std namespace.

    You can also tell the compiler to only look for one name with "using std::<name>;" where <name> would be something like cout or endl, or you can tell the compiler what namespace to look in with every use by prefixing the name with std::. The following program demonstrates the last two.
    Code:
    #include <iostream>
    
    using std::cout;
    
    int main()
    {
      cout<<"Hello world!"<<std::endl;
    }
    Notice how cout isn't prefixed because I already told the compiler to look for "std::cout" whenever it sees "cout". endl has to be prefixed because otherwise the compiler won't know that "endl" is actually "std::endl".
    Kampai!

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    This line is bringing the std namespace into scope. Almost all of the functions / objects declared in iostream and various other header files are declared in the std namespace to avoid name clashing. Without doing that when you write cout the compiler doesn't now what you are refering to.

    Here is a thread that discussed namespaces recently and I think it did a decen job. Let me know if you have further questions.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  4. #4
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    It declares that you will use cout and cin and vector and EveryStdYouCanThinkOf instead of std::Whatever.

    The thing is, cout and cin are really std::cout and std::cin. This saves you from having to type up std::Whatever everytime you want to use it (which is sometimes several hundred times in larger programs.) It saves a lot of time and effort.

    And now time for a story

    Once upon my time I went through a rebellious stage in my coding. I didn't use using namespace std; and used gotos and global variables anywhere I could. I was a bad boy.

    One day I wrote a RPG and when I compiled it there were about, um, 219 and a half errors (more or less). After that, um, incident I read everything I could about proper coding and my code was perfect for a month. After that I went back to coding normally.

    THE END

    EDIT: D'oh! I hate when I tell stories when there are no posts and when I finish remembering my learning-of-C++-hood there are two other posts. NO!!!!!
    Last edited by fuh; 01-30-2005 at 09:43 AM.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  5. #5
    LBY
    LBY is offline
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Hmm ok I think I understood it, I'm just not sure then why is there #include <iostream> and this too?
    by what I understood the #include <iostream> includes all the commands and the using namespace std; tells the computer something about thier names... =] ok I'm confused

    by the way fuh, nice story :P

    Edit: I read the message that andyhunter gave me, and I think I understood,
    sometimes it's easier to write the same names for variables so there are namespaces that in them you can write the same variable (in two diffrent namespaces), and you just need to write the name of the namespace before the variable so it will know which variable are you talking about... am I right?
    Last edited by LBY; 01-30-2005 at 11:59 AM.

  6. #6
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    You got it.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    580
    They explained it all very well, but I would also like to point out that you can make your own namespaces.

    namespace MyNamespace
    {
    int x;
    }

    int x; //a global variable

    MyNamespace::x is not the same as global x. They have the same name, but their namespace is different, therefore they are different variables (although their name is the same).

    Classes are also namespaces, that's why in two different classes you can have two variables with the same name, but they are different variables because they are in separate classes (different namespaces).

    It's akin to having the same numerical value of a number, but the units may be different. Maybe not the best example but it sort of works.
    Last edited by Darkness; 01-30-2005 at 03:10 PM.
    See you in 13

  8. #8
    LBY
    LBY is offline
    Registered User
    Join Date
    Nov 2004
    Posts
    12
    Ok I understood, I was about to ask that isn't it pretty much like structures, but never mind I reminded in what structures are diffrent, so I think I understood all about it, thanks alot for all that helped =]

  9. #9
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Also, <iostream.h> and <iostream> are two different things. <iostream> is included in the std namespace, which is why you have to include 'using namespace std' or specify the namespace before you use each iostream command.

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, 04:10 PM
  2. std:: or namespace std
    By C-+ in forum C++ Programming
    Replies: 16
    Last Post: 12-04-2007, 12:30 PM
  3. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:17 AM
  4. need ; before using namespace std
    By wwfarch in forum C++ Programming
    Replies: 7
    Last Post: 05-11-2004, 11:42 PM
  5. Site tutorials
    By Aalmaron in forum C++ Programming
    Replies: 20
    Last Post: 01-06-2004, 02:38 PM

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