Thread: Vector in c++ stl

  1. #31
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    well i have described the problem correctly.....

    Laserlight...Annon understood too....no problem with it....

    well, Laserlight........ the code seems to work different the other case...can u say why?????

  2. #32
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    the code seems to work different the other case...can u say why?
    What code are you comparing? How does it "work different"?
    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

  3. #33
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    this one
    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);
    }
    works fine....
    but instead of hardcoding j value...if i let the user input "j" each time it works differently

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    but instead of hardcoding j value...if i let the user input "j" each time it works differently
    What is the purpose of allowing the user to input the value of j?
    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

  5. #35
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    anything should have a global solution right???

    it cant just be escapism from errors....

    tat s y i ask

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    anything should have a global solution right???

    it cant just be escapism from errors....
    Sorry, I do not understand what you are trying to say. Solution to what? What is so global about it?

    The way I see it, you have never even properly defined the problem that you are trying to solve in this thread. All you have done is present some cobbled together code that we have suggested fixes for. So, of course what we are suggesting is just "escapism from errors".

    If you actually told us what you were trying to do, we could show you correct ways of solving your problem. At the moment, it is a game of guess and check, and I am getting tired of it.
    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

  7. #37
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    i dont mean that the suggestions were escapism.... They are definitely solutions...

    am jsut stating that any problem has gloabal solution cos u stated
    What is the purpose of allowing the user to input the value of j?
    i never meant anything wrong.
    dont mistake me...
    infact its u who u solved it half way

  8. #38
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's a long thread, but why are you using a vector to store vectors, each of which will contain one item. Why not just a vector<int>?

    You can also input how many numbers you want to input and use that value to control the loop instead of while(1). Just add another variable for 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).

  9. #39
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    but annon my question is ...If they work for some cases ...it should work for other cases too...

    why is this not happening...?????

    i mean wat laserlight suggested in the code..
    but when i use CIN>>J and do
    g[j].push_back(..................
    it doesnt work

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you allow the user to enter some value for j, then they could enter a value such that j is not a valid index for g.

    If you do want the user to be able to enter some arbitrary index and associate it with some value, then perhaps you should use a std::map or std::tr1::unordered_map instead of a std::vector (though a std::vector may still be appropriate if the range of keys is small and dense enough).
    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. #41
    Registered User
    Join Date
    Jan 2009
    Posts
    197
    huh Laserlight even with the assumption that valid j value is entered by the user that doesnt seem to work as expected...that s my problem

  12. #42
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why is the user typing an index value for the outer vector? What exactly is the point? What are you trying to do?
    You cannot access an index that doesn't exist yet. You use push_back() to add new rows to your vector. If for some reason you want to add a bunch of rows initialized all to the same value, use resize(). Otherwise it's like trying to withdraw $1,000,000 from a bank account that only has $1000 in it. It just won't work.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #43
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have a situation where you want the user to put in things in "sparse matrix" (that is, you only have a few elements scattered here and there in a larger set of coordinates) then a std::map may be a better option.

    --
    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.

  14. #44
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dpp
    huh Laserlight even with the assumption that valid j value is entered by the user that doesnt seem to work as expected...that s my problem
    That implies that your program has at least one bug and/or you did not actually satisfy the assumption.

    Frankly, I think that you should take my suggestion of using a std::map or std::tr1::unordered_map instead.

    No wait, I think that you should think through what you are trying to do instead of playing a game of guess and check.
    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. 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