You are confusing the using directive with the #include directive
Originally Posted by tezcatlipooca
This is what brings the std::vector class in so you are able to use it. What you are bringing into the program in this way is a class definition for a class called vector that is in the std namespace.
Then, to declare a variable of type std::vector you need to tell the compiler that the variable is of type std::vector, just like you would tell it a variable is of type int or float.
You can think of namespaces as a fancy way of making variable and class names longer in a uniform way so that, even if you write a class that should logically be called vector (like a program dealing with motion), as long as you use the namespaces correctly both vector classes can exist without the compiler getting confused as to which one you mean.
Once you declare v above as a vector of int's, you can access the push_back() function (or any other public function from the std::vector class) with the dot operator
Notice I have not used a using statement anywhere. You don't need to have any using directives in your code. When the compiler sees std::vector<int> v; it looks in the std namespace for the vector<int> and creates v according to that definition. Since push_back() is part of that definition the compiler knows how to perform a push_back() whenever v.push_back() is called without any additional assistance.
x = 12;
std::cout << v.at(0);
If it is obvious that your code is going to use the word vector only to mean the std::vector class, then you can put
in your code, and thereafter the compiler will know you mean std::vector any time it sees vector. That line is like telling the compiler, "There is something in the std namespace called vector, so if you see vector with no namespace in front of it that's the one I am using".
Consider the following for a flight simulator where the programmer has created a class called vector to keep track of the different airplanes flying around. To better prevent a name collision with the std::vector class, and also make it easier to distinguish between them, this program has put the airplane vector class into the namespace airplane. This namespace contains a bunch of other airplane related classes just like the std namespace contains a bunch of other "standard" classes and functions that tend to be used in many C++ programs:
The first one clearly identifies which vector you mean via the namespaces. The other 2 would work but could become confusing, especially if there is a lot of code between the using directive and the line that has just vector in it. In these cases it would not be as clear which vector you are using and would only serve to make the code harder to read or maintain.
std::vector< airplane::vector > vecarray; // an array of airplane vectors
vector< airplane::vector > vecarray;
std::vector< vector > vecarray;