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?
Printable View
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?
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?
So basicly you never "have" to use std::?
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;
}
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.
Thanks.
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");
}