Thread: Vectors question

  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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    brings the whole std::vector class in scope so i am able to use any function declared in it.
    It brings the name vector into scope such that you no longer have to prefix it with a std::

    You will still not be able to directly use all private (and protected) members of std::vector, whether member function or member variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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