| Author |
char[8] as map key
|
|
|
| Can a map handle a static array as a key?
For example:
map < unsigned long[8], CSomeOtherClass > items;
I could always use a vector<unsigned long> but I was wondering if it the map
was smart enough to make the comparison on just a static array.
| |
| Arnaud Debaene 2005-08-30, 7:03 pm |
| TGD wrote:
> Can a map handle a static array as a key?
>
> For example:
>
> map < unsigned long[8], CSomeOtherClass > items;
>
> I could always use a vector<unsigned long> but I was wondering if it
> the map was smart enough to make the comparison on just a static
> array.
No. Even if you provide a Traits parameter to the map class that can compare
and order C-style arrays, the key of a map must be assignable, which a
C-array is not.
Arnaud
MVP - VC
| |
| Pete Becker 2005-08-30, 7:03 pm |
| TGD wrote:
> Can a map handle a static array as a key?
>
> For example:
>
> map < unsigned long[8], CSomeOtherClass > items;
>
> I could always use a vector<unsigned long> but I was wondering if it the map
> was smart enough to make the comparison on just a static array.
>
As Arnaud said, an array isn't copyable. But a struct that holds an
array is:
struct wrapper
{
unsigned long values[8];
};
bool operator<(const wrapper& left, const wrapper& right)
{
// your code goes here
}
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
| |
| Brian Muth 2005-08-30, 7:03 pm |
| struct MyLongs
{
unsigned long x[8];
};
map<MyLongs, CSomeOtherClass> items;
should work.
Brian
| |
| Brian Muth 2005-08-30, 7:03 pm |
| Peter gave the more complete answer, as I left out the compare operation.
| |
|
|
"Pete Becker" <petebecker@acm.org> wrote in message
news:BdudnWJRmO8AAIneRVn-gA@rcn.net...
> TGD wrote:
>
>
> As Arnaud said, an array isn't copyable. But a struct that holds an array
> is:
>
> struct wrapper
> {
> unsigned long values[8];
> };
>
> bool operator<(const wrapper& left, const wrapper& right)
> {
> // your code goes here
> }
Wouldn't the comparison function need to be in a function object?
| |
| Igor Tandetnik 2005-08-30, 7:03 pm |
| TGD <dropbin@hotmail.com> wrote:
> "Pete Becker" <petebecker@acm.org> wrote in message
> news:BdudnWJRmO8AAIneRVn-gA@rcn.net...
>
>
> Wouldn't the comparison function need to be in a function object?
The comparison function already is a function object. The third template
parameter of std::map defaults to std::less<Key>, implemented like this:
template <typename T>
struct less
{
bool operator()(const T& lhs, const T& rhs) const
{
return lhs < rhs; // that's where your operator<() kicks in
}
};
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925
| |
|
|
"Igor Tandetnik" <itandetnik@mvps.org> wrote in message
news:uicvhgZrFHA.2272@TK2MSFTNGP11.phx.gbl...
> TGD <dropbin@hotmail.com> wrote:
>
> The comparison function already is a function object. The third template
> parameter of std::map defaults to std::less<Key>, implemented like this:
>
> template <typename T>
> struct less
> {
> bool operator()(const T& lhs, const T& rhs) const
> {
> return lhs < rhs; // that's where your operator<() kicks in
> }
> };
>
So, rather than create a new function object for comparison I should just
define a global static comparison function for the wrapper struct itself?
| |
| David Olsen 2005-08-30, 7:03 pm |
| TGD wrote:
> So, rather than create a new function object for comparison I should just
> define a global static comparison function for the wrapper struct itself?
If operator< makes sense for your class (i.e. it does just what users of
your class would expect), you should define a global comparison function
for your class.
If operator< doesn't make sense, then you don't want a global comparison
function that would just confuse people. But you still need some sort
of ordering for the std::map, so you should create a functor to pass to
std::map.
--
David Olsen
qg4h9ykc5m@yahoo.com
| |
|
| Thanks to all who replied. It was very helpful.
|
|
|
|