Thread: Vectors question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Vectors question

    Hi,
    when i use the push_back function to add an element at the end of a vector, is vector resized automatically (if necessary) or should i check it out?
    Thanks..

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    It is resized for you.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Those containers do everything for you to make sure things don't go wrong, and if something does go wrong, an exception is thrown.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if something does go wrong, an exception is thrown.
    Not necessarily. For example, if you use an index outside of the vector's bounds with at(), it will through an exception, but if you do it with operator[] you will get undefined behavior.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Good point.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Got it. Thanks.
    May someone tell me how i should use the "using" declaration to declare the push_back function?
    I try the following but i get a compilation error:
    Code:
    using std::vector<int>::push_back;
    And what about if i want to use a vector for a data type that i define later in the file?
    I used the "extern" declaration but i also got a compilation error
    Thanks in advance!

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you think you need to "declare" the push_back function? The only case I can think of when you might want to do this is if you were inheriting from vector.

    >> And what about if i want to use a vector for a data type that i define later in the file?
    I believe the vector needs the full type information of the type it holds. You should be able to change your code so that the vector is declared after the type. If you absolutely cannot, then you will probably have to store a pointer to the datatype in the vector.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Quote Originally Posted by Daved
    Why do you think you need to "declare" the push_back function? The only case I can think of when you might want to do this is if you were inheriting from vector.

    ...
    Yeah, right, i didn't make myself clear. I just want to use the function, so i'll have to use the "using" declaration before i use it

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I just want to use the function, so i'll have to use the "using" declaration before i use it
    No, you don't need to use a using declaration before you use a function.

    You just have to make sure you've included the appropriate header that has the function declaration. In this case that's <vector>. If you're getting a specific error, feel free to post the error message and code it refers to.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Quote Originally Posted by Daved
    >> I just want to use the function, so i'll have to use the "using" declaration before i use it
    No, you don't need to use a using declaration before you use a function.

    You just have to make sure you've included the appropriate header that has the function declaration. In this case that's <vector>. If you're getting a specific error, feel free to post the error message and code it refers to.
    Well, as far as i know vector class is declared in the std namespace. And whenever i need to use a class or a function out of its namespace i shall use the "using" declaration before.
    (Obviously you disagree so i must be wrong; i'm just telling you what i have understood. Please correct me if possible)

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Whenever you use a class or a function from a namespace, you must either fully qualify it or use a using declaration or statement to make it visible, true.

    But push_back is not in the std namespace, it's in the vector class.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Quote Originally Posted by CornedBee
    Whenever you use a class or a function from a namespace, you must either fully qualify it or use a using declaration or statement to make it visible, true.

    But push_back is not in the std namespace, it's in the vector class.
    http://www.cprogramming.com/tutorial/stl/vector.html

    "Moreover, the vector class is part of the std namespace, so you must either prefix all references to the vector template with std:: or include "using namespace std;" at the top of your program."

    By using the statement
    Code:
    using std:: vector;
    i have access to every function declared in the vector class?
    Last edited by tezcatlipooca; 12-19-2006 at 07:51 PM.

  13. #13
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    No, to use the vector, there's two way:

    For example:
    1.
    Code:
    #include <vector>
    int main()
    {
      std::vector<int> myVector;
      myVector.push_back(int);
      return 0;
    }

    2.
    Code:
    #include <vector>
    
    using namespace std;
    
    int main()
    {
      vector<int> myVector;
      myVector.push_back(int);
      return 0;
    }
    Here's some reading about namespace: http://www.cprogramming.com/tutorial/namespaces.html
    Last edited by g4j31a5; 12-19-2006 at 08:49 PM.
    ERROR: Brain not found. Please insert a new brain!

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

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    However the following code compiles and runs normally:
    Code:
    #include <iostream>
    using std::cout;
    #include <vector>
    using std::vector;
    int main(){
        vector<int> x;
        x.push_back(3);
        x.push_back(5);
        cout<<"x[0]="<<x.at(0)<<" x[1]="<<x.at(1);
        system("pause");
        return 0;
        }
    What i understand is that the following statement
    Code:
    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)

  15. #15
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by tezcatlipooca
    What i understand is that the following statement
    Code:
    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
    Code:
    #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.
    Code:
    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
    Code:
    x = 12;
    v.push_back(x);
    std::cout << v.at(0);
    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
    Code:
    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:
    Code:
    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 "

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