Thread: Vectors question

  1. #16
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    What laserlight said is true. Namespace is not class. So you can't just use the methods / members inside the class without calling the class's object.
    ERROR: Brain not found. Please insert a new brain!

    Do nothing which is of no use. - Miyamoto Musashi.

  2. #17
    verbose cat
    Join Date
    Jun 2003
    Quote Originally Posted by tezcatlipooca
    What i understand is that the following statement
    using std::vector;
    brings the whole std::vector class in scope so i am able to use any function declared in it.
    (Please correct me if wrong)
    You are confusing the using directive with the #include directive
    #include <vector>
    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.
    std::vector<int> v;
    int x;
    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
    x = 12;
    std::cout <<;
    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.

    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
    using std::vector;
    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:
    std::vector< airplane::vector > vecarray;  // an array of airplane vectors
    using std::vector;
    vector< airplane::vector > vecarray;
    using airplane::vector;
    std::vector< vector > vecarray;
    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.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #18
    Registered User
    Join Date
    Dec 2006
    You've been of great help.. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about Vectors
    By ataman in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2008, 12:36 PM
  2. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 06:31 PM
  3. question about stacks (and vectors too for that matter)
    By Silvercord in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2003, 12:26 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. vector of vectors question
    By PJYelton in forum C++ Programming
    Replies: 5
    Last Post: 02-10-2003, 03:02 PM