By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
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<vector<int values;

I tried to add the value 35 to the first position of the first vector
and this vector should be on the first position of the "main" vector.
How can I push_back() a vector which contains ints to a vector?
I tried it in a different way like:

vector<intxAxis;
vector<xAxisyAxis;
But I get compiling errors. I thought about using resize() but both
vectors will be resized in a loop so I have to resize them over and over
again. Moreover I didn't find a conclusion with resize() either. Why do
I actually have to resize them although vectors are dynamic types?
And how can I solve this issue?
Markus
Oct 28 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Markus Pitha" <ne**************@pithax.netwrote in message
news:14***************************@news.chello.at. ..
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<vector<int values;

I tried to add the value 35 to the first position of the first vector and
this vector should be on the first position of the "main" vector.
How can I push_back() a vector which contains ints to a vector?
I tried it in a different way like:

vector<intxAxis;
vector<xAxisyAxis;
But I get compiling errors. I thought about using resize() but both
vectors will be resized in a loop so I have to resize them over and over
again. Moreover I didn't find a conclusion with resize() either. Why do I
actually have to resize them although vectors are dynamic types?
And how can I solve this issue?
This help any?

#include <vector>

int main()
{
std::vector<std::vector<int Data;
std::vector<intRow;
Data.push_back( Row );

Data[0].push_back( 11 );
Data[0].push_back( 12 );

if ( Data.size() < 2 )
Data.push_back( Row );
Data[1].push_back( 21 );
Data[1].push_back( 22 );
}
Oct 28 '07 #2

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<intaVector(1, 35); // initialized to have 1 value, 35
values.push_back(aVector);
I tried it in a different way like:

vector<intxAxis;
vector<xAxisyAxis;
I don't really understand what is it that you tried to accomplish
with that. If what you want is to create an x*y double-vector, you can
do it like this:

std::vector<std::vector<int values(xSize, std::vector<int>(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 <vector>

int main()
{
std::vector<std::vector<int Data;
std::vector<intRow;
Data.push_back( Row );

Data[0].push_back( 11 );
Data[0].push_back( 12 );

if ( Data.size() < 2 )
Data.push_back( Row );
Data[1].push_back( 21 );
Data[1].push_back( 22 );
}
Thanks, that helped me to solve my problem.

Markus
Oct 28 '07 #4

P: n/a
Hello,

Juha Nieminen wrote:
std::vector<intaVector(1, 35); // initialized to have 1 value, 35
values.push_back(aVector);
std::vector<std::vector<int values(xSize, std::vector<int>(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" <ne**************@pithax.netwrote in message
news:28**************************@news.chello.at.. .
Hello,

Jim Langston wrote:
>#include <vector>

int main()
{
std::vector<std::vector<int Data;
std::vector<intRow;
Data.push_back( Row );

Data[0].push_back( 11 );
Data[0].push_back( 12 );

if ( Data.size() < 2 )
Data.push_back( Row );
Data[1].push_back( 21 );
Data[1].push_back( 22 );
}

Thanks, that helped me to solve my problem.
Incidently, if you don't want to have to create a variable to push back you
could just do:

#include <vector>

int main()
{
std::vector<std::vector<int Data;
std::vector<intRow;
Data.push_back( std::vector<int>() );

Data[0].push_back( 11 );
Data[0].push_back( 12 );

if ( Data.size() < 2 )
Data.push_back( std::vector<int>() );
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<intxAxis;
Here you are declaring a variable xAxis.
vector<xAxisyAxis;
Here you treat xAxis as a type name.

It should be
typedef vector<intxAxis;
vector<xAxisyAxis;
to compile, if I correctly guess what you mean.
Oct 28 '07 #7

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<intaVector(1, 35); // initialized to have 1 value, 35
values.push_back(aVector);
I tried it in a different way like:

vector<intxAxis;
vector<xAxisyAxis;

I don't really understand what is it that you tried to accomplish
with that. If what you want is to create an x*y double-vector, you can
do it like this:

std::vector<std::vector<int values(xSize, std::vector<int>(ySize));
For that, all you need is:

size_t xSize( 20 ), ySize( 30 );
std::vector<std::vector<int values( xSize, ySize ); // 20x30

--
Bob R
POVrookie
Oct 29 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.