Sizing a vector of vectors

C

cheeser

Hello,

I'm trying to size a vector of vectors of unsigned ints to be an NxN square.
Here's how I'm doing it:



typedef vector<vector<unsigned int> > tournament_type;

unsigned int n;
tournament_type tournament;

// Go get a value for n...

tournament.resize(n);

for_each(
tournament.begin(),
tournament.end(),
bind2nd(mem_fun1(&tournament_type::value_type::resize), n)
);



On the platform I'm on (VC++ 7.1), I get an internal compiler error. Before
I go over to the VC++ group, I'd like to make sure this code should indeed
compile. Can anybody see any syntactical or semantic errors that I have
made? Or is it OK, indicating a true compiler problem?

Thanks,
Dave
 
M

Mike Wahler

cheeser said:
Hello,

I'm trying to size a vector of vectors of unsigned ints to be an NxN square.
Here's how I'm doing it:



typedef vector<vector<unsigned int> > tournament_type;

unsigned int n;
tournament_type tournament;

// Go get a value for n...

tournament.resize(n);

for_each(
tournament.begin(),
tournament.end(),
bind2nd(mem_fun1(&tournament_type::value_type::resize), n)
);



On the platform I'm on (VC++ 7.1), I get an internal compiler error. Before
I go over to the VC++ group, I'd like to make sure this code should indeed
compile. Can anybody see any syntactical or semantic errors that I have
made? Or is it OK, indicating a true compiler problem?

I won't even try to diagnose that, since I'm not sure
what you think that's supposed to do. Try something
like this:

#include <iostream>
#include <vector>

template<typename T>
void squarevec(std::vector<std::vector<T> >& v,
std::vector<std::vector<T> >::size_type sz)
{
v = std::vector<std::vector<T> >(sz, std::vector<T>(sz));
}

template<typename T>
void showsizes(const std::vector<std::vector<T> >& v)
{
std::cout << "v.size() == " << v.size() << '\n';
for(std::vector<std::vector<T> >::size_type i = 0;
i != v.size(); ++i)
{
std::cout << "v" << '[' << i << "].size() == "
<< v.size() << '\n';
}
}

int main()
{
std::vector<std::vector<unsigned int> >::size_type howmany(5);
std::vector<std::vector<unsigned int> > vec;

squarevec(vec, howmany);
showsizes(vec);
return 0;
}

Output:

v.size() == 5
v[0].size() == 5
v[1].size() == 5
v[2].size() == 5
v[3].size() == 5
v[4].size() == 5

-Mike
 
C

cheeser

cheeser said:
Hello,

I'm trying to size a vector of vectors of unsigned ints to be an NxN square.
Here's how I'm doing it:



typedef vector<vector<unsigned int> > tournament_type;

unsigned int n;
tournament_type tournament;

// Go get a value for n...

tournament.resize(n);

for_each(
tournament.begin(),
tournament.end(),
bind2nd(mem_fun1(&tournament_type::value_type::resize), n)
);



On the platform I'm on (VC++ 7.1), I get an internal compiler error. Before
I go over to the VC++ group, I'd like to make sure this code should indeed
compile. Can anybody see any syntactical or semantic errors that I have
made? Or is it OK, indicating a true compiler problem?

Thanks,
Dave

An update to my earlier post:

Actually, I think this is the way it should be done:

for_each(
tournament.begin(),
tournament.end(),
bind2nd(mem_fun_ref(&tournament_type::value_type::resize), n)
);

The reference I have has a typo I believe. However, I still get an internal
error on VC++ 7.1 whereas it works under g++.

I'm sure someone will point out that an internal error is a problem in any
case, and they're right. I'll pass this on to MSFT. Even if there is a
legitimate problem in the code, internal error is not the proper response...

However, I'd still like to get confirmation from some of the gurus out there
that what I've done is correct according to the standard...

Thanks again,
Dave
 
C

cheeser

#include <iostream>
#include <vector>

template<typename T>
void squarevec(std::vector<std::vector<T> >& v,
std::vector<std::vector<T> >::size_type sz)
{
v = std::vector<std::vector<T> >(sz, std::vector<T>(sz));
}

template<typename T>
void showsizes(const std::vector<std::vector<T> >& v)
{
std::cout << "v.size() == " << v.size() << '\n';
for(std::vector<std::vector<T> >::size_type i = 0;
i != v.size(); ++i)
{
std::cout << "v" << '[' << i << "].size() == "
<< v.size() << '\n';
}
}

int main()
{
std::vector<std::vector<unsigned int> >::size_type howmany(5);
std::vector<std::vector<unsigned int> > vec;

squarevec(vec, howmany);
showsizes(vec);
return 0;
}

Output:

v.size() == 5
v[0].size() == 5
v[1].size() == 5
v[2].size() == 5
v[3].size() == 5
v[4].size() == 5


A perfectly valid, and reusable, approach that I hadn't considered. Thanks
Mike!
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top