constructing nested vectors

A

Anjo Gasa

If I have the following class member:

std::vector<float> singleVector;

I can initialize it to have 50 elements with values of 0.5:

singleVector = std::vector<float>( 50, 0.5 );

My question concerns when I move to using this class member:

std::vector<std::vector<float>> nestedVector;

and attempt to initialize:

nestedVector = std::vector<std::vector<float>>( 50,
std::vector<float>( 10, 0.5 ) );

which I would expect would initialize the vector to have 50 nested
vectors, each having 10 elements with values of 0.5.

This does not, however, compile.

Anjo
 
A

Alf P. Steinbach

* Anjo Gasa:
If I have the following class member:

std::vector<float> singleVector;

I can initialize it to have 50 elements with values of 0.5:

singleVector = std::vector<float>( 50, 0.5 );

My question concerns when I move to using this class member:

std::vector<std::vector<float>> nestedVector;

and attempt to initialize:

nestedVector = std::vector<std::vector<float>>( 50,
std::vector<float>( 10, 0.5 ) );

which I would expect would initialize the vector to have 50 nested
vectors, each having 10 elements with values of 0.5.

This does not, however, compile.

Spaces are extra cheap these days, try one!

Cheers,

- Alf
 
B

BobR

Anjo Gasa said:
If I have the following class member:
std::vector<float> singleVector;
I can initialize it to have 50 elements with values of 0.5:
singleVector = std::vector<float>( 50, 0.5 );
My question concerns when I move to using this class member:
std::vector<std::vector<float>> nestedVector;
and attempt to initialize:
nestedVector = std::vector<std::vector<float>>( 50,
std::vector<float>( 10, 0.5 ) );
which I would expect would initialize the vector to have 50 nested
vectors, each having 10 elements with values of 0.5.
This does not, however, compile.
Anjo

If possible, do it all in one shot:

std::vector<std::vector<float> > nestedVector( 50,
std::vector<float>( 10, 0.5 ) );

Note what Alf said about the 'space':

// .....float>> // no space, no good
.....float> > // that's '>' + ' ' + '>'

Otherwise it's taken as a 'right-shift'(7 >> 2) or 'insertion'(cin >>
intvar) operator, and the compiler has a heart attack because it can't
figure out what you are trying to shift/insert from-to.
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top