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)); } }
This is a discussion on Vector in c++ stl within the C++ Programming forums, part of the General Programming Boards category; It looks like you are trying to do this: Code: #include<iostream> #include<vector> using namespace std; int main() { vector< vector<long ...
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.
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
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
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:Originally Posted by dpp
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); } }
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
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![]()
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?Originally Posted by dpp
If you need a linked list, use std::list instead of std::vector.
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
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.
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.
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
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.Originally Posted by dpp
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; }
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
s u told at the begining...but i cud not find a way to solve it yet
even this gives the same runtime error
this is wat u told int the last postCode:#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); }
u might run it urself and see if u dont get me
Try this:
Notice that I only increment j after the push back.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); }
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
thanks it works for a simple example.....
but i shud know wat does this mean
i have so much to do with this...so ineed to know wat it actually doesG.push_back(vector<long long int> ());
plz
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.
<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"
but the same code works diffrent if i scan " j "and push it to the vector
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