 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cheeser Guest
|
Posted: Sun Sep 28, 2003 8:57 pm Post subject: Sizing a vector of vectors |
|
|
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 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
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Sep 28, 2003 9:33 pm Post subject: Re: Sizing a vector of vectors |
|
|
"cheeser" <cheeser_1998 (AT) yahoo (DOT) com> wrote
| Quote: |
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 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& v,
std::vector<std::vector::size_type sz)
{
v = std::vector<std::vector(sz, std::vector<T>(sz));
}
template<typename T>
void showsizes(const std::vector<std::vector& v)
{
std::cout << "v.size() == " << v.size() << 'n';
for(std::vector::size_type i = 0;
i != v.size(); ++i)
{
std::cout << "v" << '[' << i << "].size() == "
<< v[i].size() << 'n';
}
}
int main()
{
std::vector::size_type howmany(5);
std::vector<std::vector 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
|
|
| Back to top |
|
 |
cheeser Guest
|
Posted: Sun Sep 28, 2003 9:37 pm Post subject: Re: Sizing a vector of vectors |
|
|
"cheeser" <cheeser_1998 (AT) yahoo (DOT) com> wrote
| Quote: |
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 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
|
|
| Back to top |
|
 |
cheeser Guest
|
Posted: Sun Sep 28, 2003 9:46 pm Post subject: Re: Sizing a vector of vectors |
|
|
| Quote: | #include <iostream
#include
template
void squarevec(std::vector& v,
std::vector<std::vector::size_type sz)
{
v = std::vector<std::vector(sz, std::vector<T>(sz));
}
template<typename T
void showsizes(const std::vector& v)
{
std::cout << "v.size() == " << v.size() << 'n';
for(std::vector::size_type i = 0;
i != v.size(); ++i)
{
std::cout << "v" << '[' << i << "].size() == "
v[i].size() << 'n';
}
}
int main()
{
std::vector::size_type howmany(5);
std::vector<std::vector 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!
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|