Vector in c++ stl

This is a discussion on Vector in c++ stl within the C++ Programming forums, part of the General Programming Boards category; hi...i ve been used to vectors stl but is it nececssary to mention the size of the vector in the ...

  1. #1
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Vector in c++ stl

    hi...i ve been used to vectors stl
    but is it nececssary to mention the size of the vector in the declararion...

    can i not leave it unspecified...dynamically increasing as and wen i push items to it....

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, you can.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by dpp
    but is it nececssary to mention the size of the vector in the declararion...
    No.

    Quote Originally Posted by dpp
    can i not leave it unspecified...dynamically increasing as and wen i push items to it...
    You can indeed increase the size of a std::vector at run time with push_back and other member functions.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    thanks...then how will my declaration be...with an example plz

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Perhaps post what you have tried? You'll declare an empty vector like any other object with default constructor.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    simple eg:
    vector<long long int>g(1000);

    i get error if i use g()

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by dpp
    i get error if i use g()
    It should be:
    Code:
    vector<long long int> g;
    This declares a function named g that takes no arguments and returns a vector:
    Code:
    vector<long long int> g();
    Note that at the moment long long int is not a standard C++ built-in type.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    thanks...but i dont get it right with normal push_back function....
    anythng else needed

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by dpp
    but i dont get it right with normal push_back function.
    That's too bad, I use it and it works.

    Quote Originally Posted by dpp
    anythng else needed
    Yes.

    (If you don't get what I am hinting at: post what you tried and state how exactly does it not work.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    it works well normally for simple eg....
    when i include in my program i get an error....
    thanks...
    i need to check with it

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    If you can't get it to work, show the code. We can guess, but we can't be certain and it's by far the fastest and best method of making us give you the help you're looking for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    ya ok...say for eg:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        int i,j=0;
        //vector<<int>  g;
          vector < vector<long long int> > g;
        while(1)
        {
                j++;
         cin>>i;
         if(i==-1)break;
         g[j].push_back(i);
         }
         return(0);
    }
    i get runtime error

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    g is empty so you cannot insert into g[0].
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    ya that is the reasion i used g(maxvalue) in declaration...
    but how can i leave it unbounded

  15. #15
    dpp
    dpp is offline
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    i want it to be unspecified bound and increasing as and when i push elements....
    can i do it...
    if so how

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 04:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21