Another question about multidimensional vectors

M

Markus Pitha

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<int> xAxis;
vector<xAxis> yAxis;


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
 
J

Jim Langston

Markus Pitha said:
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<int> xAxis;
vector<xAxis> yAxis;


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<int> Row;
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 );
}
 
J

Juha Nieminen

Markus said:
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:

I tried it in a different way like:

vector<int> xAxis;
vector<xAxis> yAxis;

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

Markus Pitha

Hello,

Jim said:
#include <vector>

int main()
{
std::vector<std::vector<int> > Data;
std::vector<int> Row;
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
 
M

Markus Pitha

Hello,

Juha said:
std::vector<int> aVector(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
 
J

Jim Langston

Markus Pitha said:
Hello,

Jim said:
#include <vector>

int main()
{
std::vector<std::vector<int> > Data;
std::vector<int> Row;
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<int> Row;
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 );
}
 
T

Tadeusz Kopec

Markus Pitha wrote:
[snip]
I tried it in a different way like:

vector<int> xAxis;
Here you are declaring a variable xAxis.
vector<xAxis> yAxis;
Here you treat xAxis as a type name.

It should be
typedef vector<int> xAxis;
vector<xAxis> yAxis;
to compile, if I correctly guess what you mean.
 
B

BobR

Juha Nieminen wrote in message...
Markus said:
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:

I tried it in a different way like:

vector<int> xAxis;
vector<xAxis> yAxis;

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
 
J

James Kanze

Juha Nieminen wrote in message...

[...]
For that, all you need is:
size_t xSize( 20 ), ySize( 30 );
std::vector<std::vector<int> > values( xSize, ySize ); // 20x30

Are you sure? The actual wording in the standard says that this
should work, but the fact that it works is generally considered
to be an accidental side effect of the way the paragraph was
formulated, and is the subject of DR 438. The current status of
DR 438 is still DR, which means that it is still under
discussion, but according to the documentation of the issue,
there seems to be a consensus that this shouldn't work, and that
the only reason the issue is still open is that there are
problems finding the exact words to replace those in the
standard.

In the meantime, the code will compile with some library
implementations, and not with others. (It compiles with 2 out
of 4 that I have access to.) Given that its status is unsure,
however, I'd avoid it; once the wording is finalized, it's quite
possible (even probable) that it will cease working in later
versions of the compilers which currently support it.
 
B

BobR

James Kanze wrote in message...
/* """
Juha Nieminen wrote in message...
For that, all you need is:
size_t xSize( 20 ), ySize( 30 );
std::vector<std::vector<int> > values( xSize, ySize ); // 20x30

Are you sure? The actual wording in the standard says that this
should work, but the fact that it works is generally considered
to be an accidental side effect of the way the paragraph was
formulated, and is the subject of DR 438. The current status of
DR 438 is still DR, which means that it is still under
discussion, but according to the documentation of the issue,
there seems to be a consensus that this shouldn't work, and that
the only reason the issue is still open is that there are
problems finding the exact words to replace those in the
standard.

In the meantime, the code will compile with some library
implementations, and not with others. (It compiles with 2 out
of 4 that I have access to.) Given that its status is unsure,
however, I'd avoid it; once the wording is finalized, it's quite
possible (even probable) that it will cease working in later
versions of the compilers which currently support it.
""" */

That's a real shame. With 2D arrays so common, it sure *was* handy.
I'd vote to keep it. How much trouble could it cause? If GCC can do it, it
should be easy for other implementors to copy (I can still provide a default
value when needed (modifying Juha's example).).

Are we eliminating practical things in order to provide wording in the
standard?
<G>

Anyway, thanks for the info.
 
W

Wade Ward

BobR said:
James Kanze wrote in message...
/* """




Are you sure? The actual wording in the standard says that this
should work, but the fact that it works is generally considered
to be an accidental side effect of the way the paragraph was
formulated, and is the subject of DR 438. The current status of
DR 438 is still DR, which means that it is still under
discussion, but according to the documentation of the issue,
there seems to be a consensus that this shouldn't work, and that
the only reason the issue is still open is that there are
problems finding the exact words to replace those in the
standard.

In the meantime, the code will compile with some library
implementations, and not with others. (It compiles with 2 out
of 4 that I have access to.) Given that its status is unsure,
however, I'd avoid it; once the wording is finalized, it's quite
possible (even probable) that it will cease working in later
versions of the compilers which currently support it.
""" */

That's a real shame. With 2D arrays so common, it sure *was* handy.
I'd vote to keep it. How much trouble could it cause? If GCC can do it, it
should be easy for other implementors to copy (I can still provide a
default
value when needed (modifying Juha's example).).

Are we eliminating practical things in order to provide wording in the
standard?
<G>
Words are important.

We talk ad nauseum in comp.lang.fortran about C. Is fortran on the map for
C++; in other words, is it mentioned in the standard? I think C++ were
different than C, where the only mention of fortran is that it is a common
extension. I don't see how a person could claim that fortran were a common
c++ extension. Indeed, it would seem like more of a cousin than direct
spawning.

Sometimes we talk about c++. I lurk on such conversations, and if I violate
this policy, I say something that is wrong. A fair amount of talk about
interoperation in modern prog. lang's has to do with how vectors and arrays
behave. When people who actually know standard c++ talk, they are concerned
with the vectors that I think look like this when they are defined:

I'm guessing that this is a standard way to get a 20 by thirty array.
Certainly, in the C++ Standard there is talk about, for example, alignment
considerations. Where are they?

--
wade ward

President
Merrill Jensen Consulting


(e-mail address removed)
435 -838-7760
--
 
J

James Kanze

James Kanze wrote in message...
That's a real shame. With 2D arrays so common, it sure *was* handy.
From the point of view of the design of std::vector, of course,
it sure is a hack. It only works because it converts the size_t
into a vector<T>, despite the fact that the constructor is
declared explicit.

Of course, hacks can be useful things; pragmatics rule. But try
to explain why it works to a beginner.
I'd vote to keep it. How much trouble could it cause? If GCC
can do it, it should be easy for other implementors to copy (I
can still provide a default value when needed (modifying
Juha's example).).
Are we eliminating practical things in order to provide
wording in the standard?

I'd vote to keep it, too, if for no other reason than removing
it does represent a significant change in what used to be
guaranteed; I don't think it belongs there logically, and it
doesn't extend to further dimensions.

On the other hand, I wouldn't oppose some real support for
multi-dimension arrays. It's not that hard to implement, and
something standard would seem to be in order---something which
guarantees that all of the rows have the same length, for
example. But I've not seen a proposal along these lines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top