Connecting Tech Pros Worldwide Forums | Help | Site Map

Sizing a vector of vectors

cheeser
Guest
 
Posts: n/a
#1: Jul 19 '05

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::res ize), 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



Mike Wahler
Guest
 
Posts: n/a
#2: Jul 19 '05

re: Sizing a vector of vectors



"cheeser" <cheeser_1998@yahoo.com> wrote in message
news:vTHdb.3206$La.1055@fed1read02...[color=blue]
>
> Hello,
>
> I'm trying to size a vector of vectors of unsigned ints to be an NxN[/color]
square.[color=blue]
> 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::res ize), n)
> );
>
>
>
> On the platform I'm on (VC++ 7.1), I get an internal compiler error.[/color]
Before[color=blue]
> 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?[/color]

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[i].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



cheeser
Guest
 
Posts: n/a
#3: Jul 19 '05

re: Sizing a vector of vectors



"cheeser" <cheeser_1998@yahoo.com> wrote in message
news:vTHdb.3206$La.1055@fed1read02...[color=blue]
>
> Hello,
>
> I'm trying to size a vector of vectors of unsigned ints to be an NxN[/color]
square.[color=blue]
> 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::res ize), n)
> );
>
>
>
> On the platform I'm on (VC++ 7.1), I get an internal compiler error.[/color]
Before[color=blue]
> 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
>
>[/color]

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


cheeser
Guest
 
Posts: n/a
#4: Jul 19 '05

re: Sizing a vector of vectors


[color=blue]
> #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[i].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[/color]

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


Closed Thread