Thread: Vector in c++ stl

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks like you are trying to do this:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        vector< vector<long long int> > g;
        for (;;)
        {
            int i;
            cin >> i;
            if (i == -1)
            {
                break;
            }
    
            g.push_back(vector<long long int>(1, i));
        }
    }
    Last edited by laserlight; 05-10-2009 at 11:27 PM.
    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

  2. #17
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    no...not this
    my requirement is wat i mentioned before...
    ofcourse it works well with bounds
    but i am not able to leave it unspecified

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    ofcourse it works well with bounds
    but i am not able to leave it unspecified
    The bounds of g are "unspecified". I merely demonstrated how to push back a vector of size 1 as an element of g because it makes sense in your example. If you insist:
    Code:
    #include<iostream>
    #include<vector>
    using namespace std;
    int main()
    {
        vector< vector<long long int> > g;
        for (;;)
        {
            int i;
            cin >> i;
            if (i == -1)
            {
                break;
            }
    
            g.push_back(vector<long long int>());
            g.back().push_back(i);
        }
    }
    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

  4. #19
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    but this is not i want

    wat i wanted was the code above i posted....
    its similar to linked list ...are u able to undersatnd my code...its like that of a linked list...


    the code u posted is not wat i require

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    wat i wanted was the code above i posted....
    its similar to linked list ...are u able to undersatnd my code...its like that of a linked list...
    I specifically wrote both my examples to approximate what I thought you were trying to do in your example. Apparently I thought wrong. What exactly are you trying to do?

    If you need a linked list, use std::list instead of std::vector.
    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

  6. #21
    The larch
    Join Date
    May 2006
    Posts
    3,573
    From your example it is quite impossible to tell what you "require".

    In any case, if the item with some index is not in the vector, you can't access it just like that.
    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).

  7. #22
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    hello annon....
    i am just asking whetheri can do it this way........
    see it works if i declare it as g(100)
    and use g[0]
    and i ve succesfully using this method for quite some time.....
    i ve been using this for quite a long time...so there s no looking back.....
    but wat i want now is g with bound unspecified .....
    all i wanted to know whther i can leave the bound unspecified or not

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    but wat i want now is g with bound unspecified .....
    all i wanted to know whther i can leave the bound unspecified or not
    We have already given you the answer: yes, you can grow the vector dynamically at run time, so you do not need to specify the bounds of the vector when you create it.

    Maybe a simpler example with a vector of vectors will help:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    int main()
    {
        vector< vector<int> > v;
        v.push_back(vector<int>());
        v[0].push_back(1);
        v[0].push_back(2);
        v.push_back(vector<int>());
        v[1].push_back(3);
    
        cout << v[0][0] << endl
            << v[0][1] << endl
            << v[1][0] << endl;
    }
    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

  9. #24
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    s u told at the begining...but i cud not find a way to solve it yet

    even this gives the same runtime error
    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.push_back(vector<long long int>());
    
         g[j].push_back(i);
         }
         return(0);
    }
    this is wat u told int the last post

    u might run it urself and see if u dont get me

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try this:
    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)
        {
            cin>>i;
            if(i==-1)break;
            g.push_back(vector<long long int>());
    
            g[j].push_back(i);
            ++j;
        }
        return(0);
    }
    Notice that I only increment j after the push back.
    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

  11. #26
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    thanks it works for a simple example.....
    but i shud know wat does this mean
    G.push_back(vector<long long int> ());
    i have so much to do with this...so ineed to know wat it actually does
    plz

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to push back a vector to g first, then push back a number onto that vector (or, perhaps, push a number onto a vector, then push the vector onto g - both will work, but the former method works ALWAYS, the latter only works when adding all elements to the vector first).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by dpp View Post
    hello annon....
    i am just asking whetheri can do it this way........
    see it works if i declare it as g(100)
    and use g[0]
    and i ve succesfully using this method for quite some time.....
    i ve been using this for quite a long time...so there s no looking back.....
    but wat i want now is g with bound unspecified .....
    all i wanted to know whther i can leave the bound unspecified or not
    <rant>Is there some kind of automatic translator out there that takes as input even the most awful badly written piece of crap and converts it into ordinary readable English? Stuff with no capital letters, punctuation of any kind (except where unnecessary), horrible misspelled words and excessive abreviations, words running together, and nonsensical grammar such as shown above?
    I mean one can learn far better English of a cereal packet. Seriously, where do people who write like this come from, and what kind of twisted mayhem goes on inside their heads that would make them think that it is appropriate to write such crap here?
    Kids these days...?</rant>
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #29
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    but the same code works diffrent if i scan " j "and push it to the vector

  15. #30
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Yog Sothoth!

    Go study a tutorial, read a book, or at least describe your problem so these guys can actually help you.

    "I mean one can learn far [...]write such crap here?"

    I blame SMS messaging.

    Soma

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, 05: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