448,688 Members | 1,092 Online
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

# Another question about multidimensional vectors

 P: n/a Hello again, I have another question about the handling of multidimensional vectors. Actually, it's not difficult when the size is known at the beginning, but I still wasn't able to create dynamic vectors without knowing the size. I want to create a vector which contains a vector of ints: vector
7 Replies

 P: n/a "Markus Pitha" int main() { std::vector

 P: n/a Markus Pitha wrote: How can I push_back() a vector which contains ints to a vector? Just resize the vector and push back the int into the first vector. For example: values.resize(1); // now it has 1 int-vector inside it. values[0].push_back(35); // Pushes 35 into the first vector. If you want to *literally* push a vector of ints into 'values', you can literally do that too. For example: std::vector(ySize)); Now 'values' will be a vector with xSize vectors inside it, and each one of these will have ySize ints inside them. Then you can simply access the values like values[2][3]. Why do I actually have to resize them although vectors are dynamic types? You have to resize them *because* they are dynamic types, not *although*. Oct 28 '07 #3

 P: n/a Hello, Jim Langston wrote: #include int main() { std::vector

 P: n/a Hello, Juha Nieminen wrote: std::vector(ySize)); But I didn't know the size. The size will be increased in a recursion, so I had problems with the right syntax. Jim's example already lead my to my target. Markus Oct 28 '07 #5

 P: n/a "Markus Pitha" #include int main(){ std::vector int main() { std::vector() ); Data[0].push_back( 11 ); Data[0].push_back( 12 ); if ( Data.size() < 2 ) Data.push_back( std::vector() ); Data[1].push_back( 21 ); Data[1].push_back( 22 ); } Oct 28 '07 #6

 P: n/a Markus Pitha wrote: [snip] I tried it in a different way like: vector

 P: n/a Juha Nieminen wrote in message... Markus Pitha wrote: How can I push_back() a vector which contains ints to a vector? Just resize the vector and push back the int into the first vector. For example: values.resize(1); // now it has 1 int-vector inside it. values[0].push_back(35); // Pushes 35 into the first vector. If you want to *literally* push a vector of ints into 'values', you can literally do that too. For example: std::vector(ySize)); For that, all you need is: size_t xSize( 20 ), ySize( 30 ); std::vector

### This discussion thread is closed

Replies have been disabled for this discussion.