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?????
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?????
What code are you comparing? How does it "work different"?Originally Posted by dpp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
this oneworks fine....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); }
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?Originally Posted by dpp
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
anything should have a global solution right???
it cant just be escapism from errors....
tat s y i ask
Sorry, I do not understand what you are trying to say. Solution to what? What is so global about it?Originally Posted by dpp
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i dont mean that the suggestions were escapism.... They are definitely solutions...
am jsut stating that any problem has gloabal solution cos u stated
i never meant anything wrong.What is the purpose of allowing the user to input the value of j?
dont mistake me...
infact its u who u solved it half way
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.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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
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).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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
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.
That implies that your program has at least one bug and/or you did not actually satisfy the assumption.Originally Posted by dpp
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)