Thread: std::vector and vector, difference?

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    62

    std::vector and vector, difference?

    What's the advantages of typing "std::" before a vector initialization, for example:

    std::vector<type> instance;

    or

    vector<type> instance;

    Both seem to work, right? So what's better?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Better? Neither is better. The current standard just states that vector is within the std namespace. Thus std::vector is correct in the current standard. If both work, your compiler doesn't harp on your code matching the standard. If you want your code to be correct, keep typing std::vector or using std::vector or using namespace std in your code.

    Analogy:

    I went to the store.

    I goed to the store.

    Which is better?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    62
    So basicly you never "have" to use std::?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Two of the compilers I commonly use do not care. Reason: One is microsoft's... the other is an older compiler for making sure stuff compiles on older compilers. The other three compilers I commonly use care quite a bit and basically say they don't know what I am talking about unless I put standard (well actually the newer GCC's typically hint to you what you may have done wrong).

    To be more concise. Your compiler has the ultimate say in whether you need to put std. In your case, its not an issue. But if you want others to be able to compile your code, you may want to simply add

    Example:
    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    int main()
    {
      using namespace std;
    
      /* Code goes here */
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Unless you are using an old, non-standard compiler, you have to specify the std namespace somehow, just like with cout, cin, etc. You can use std:: in front of the name (my preference) or you can use a using directive or declaration.

    There are differences. Generally speaking std:: is safer, but in simple programs it probably doesn't matter. In larger programs it's considered bad practice not to use the std:: version in header files, which is why I use it everywhere for consistency.

    The bottom line is that if you have already made this decision for cout, cin, string, etc then follow the same advice for vector.

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    62
    Thanks.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    One is microsoft's...
    What? No.
    VS will not allow you to compile code without the "std::" prefix unless you use "using namespace std;".
    You may be thinking of an old version in that case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Microsoft's latest compiler doesn't care in some cases (specifically names from C libraries). It does give an error for vector, though.
    Code:
    #include <cstdio>
    
    int main()
    {
        printf("Hello World.\n");
    }

Popular pages Recent additions subscribe to a feed