PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Multiple index maps
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Multiple index maps

Réponse
 
LinkBack Outils de la discussion
Vieux 01/07/2008, 08h24   #1
fgh.vbn.rty@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Multiple index maps

I am frequently using maps like the following:
map<string, map<int, vector< pair<int, int> > > > m1;
map<int, map<string, map<int, int> > > m2;

This can be a little difficult to maintain if another coder doesn't
really know the what the indexes stand for, the ordering of the
indexes, etc.,

Is there a better way to define multiple index maps? Of course, I can
typedef the inner maps into something more readable but is that always
good?

Sometimes the index ordering matters and sometimes it doesn't. Does
that make a difference?

  Réponse avec citation
Vieux 01/07/2008, 08h36   #2
rep_movsd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

Can you explain what the maps represent?

That would allow us to make sensible suggestions...

Vivek
  Réponse avec citation
Vieux 01/07/2008, 08h37   #3
tharinda_g@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

On Jul 1, 12:24pm, fgh.vbn....@gmail.com wrote:
> I am frequently using maps like the following:
> map<string, map<int, vector< pair<int, int> > > > m1;
> map<int, map<string, map<int, int> > > m2;
>
> This can be a little difficult to maintain if another coder doesn't
> really know the what the indexes stand for, the ordering of the
> indexes, etc.,
>
> Is there a better way to define multiple index maps? Of course, I can
> typedef the inner maps into something more readable but is that always
> good?
>
> Sometimes the index ordering matters and sometimes it doesn't. Does
> that make a difference?


why can't you use a struct to contain the multiple keys and use a
compare functor to compare the compound key.
  Réponse avec citation
Vieux 01/07/2008, 09h22   #4
fgh.vbn.rty@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

On Jul 1, 2:36 am, rep_movsd <rep.mo...@gmail.com> wrote:
> Can you explain what the maps represent?
>
> That would allow us to make sensible suggestions...
>
> Vivek


Well, each index/key represents some property. For instance,
map<int, map<string, vector<int> > > m1;

The first int key can be something like an enum of a color type (dark,
light, etc.,) and the second string key may be the color name. So if I
wanted "dark green integers" I would need m1[DARK]["green"].

In general the indexes are independent of each other.
  Réponse avec citation
Vieux 01/07/2008, 09h24   #5
fgh.vbn.rty@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

On Jul 1, 2:37 am, tharind...@yahoo.com wrote:
> On Jul 1, 12:24 pm, fgh.vbn....@gmail.com wrote:
>
> > I am frequently using maps like the following:
> > map<string, map<int, vector< pair<int, int> > > > m1;
> > map<int, map<string, map<int, int> > > m2;

>
> > This can be a little difficult to maintain if another coder doesn't
> > really know the what the indexes stand for, the ordering of the
> > indexes, etc.,

>
> > Is there a better way to define multiple index maps? Of course, I can
> > typedef the inner maps into something more readable but is that always
> > good?

>
> > Sometimes the index ordering matters and sometimes it doesn't. Does
> > that make a difference?

>
> why can't you use a struct to contain the multiple keys and use a
> compare functor to compare the compound key.


Can you show me an example on how to construct the struct and the
compound key? Thanks.
  Réponse avec citation
Vieux 01/07/2008, 09h41   #6
Federico Zenith
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

fgh.vbn.rty@gmail.com wrote:
> I am frequently using maps like the following:
> map<string, map<int, vector< pair<int, int> > > > m1;
> map<int, map<string, map<int, int> > > m2;
>
> This can be a little difficult to maintain if another coder doesn't
> really know the what the indexes stand for, the ordering of the
> indexes, etc.,
>
> Is there a better way to define multiple index maps? Of course, I can
> typedef the inner maps into something more readable but is that always
> good?


You might make a new (simple) container class and define for it
operator(), see the following example code. If you want, you can define
an overloaded operator() that swaps the arguments, so you can both call
foo(1, "bar") and foo("bar", 1).

- --------------------------------------------
#include <iostream>
#include <string>

using namespace std;

class Foo
{
public:
void operator()( const int& number, const string& name) const;
};

void Foo:perator()( const int& number, const string& name ) const
{
cout << "I got number " << number;
cout << " and string \"" << name << "\"." << endl;
}

int main()
{
Foo foo;
foo(1, "bar");
foo(0, "baz");

return 0;
}
- ------------------------------------------

You can make the class so the data is actually stored in a map of maps,
or a maps with pairs as keys: in this way you hide the implementation
details from the user, who only sees a friendly operator, and get to use
the STL implementation.
You should also implement operator() (const) so that it returns a
(const) reference to your data, just like std::map:perator[] does.

Cheers,
- -Federico
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFIae29BIpu+y7DlLcRAnGlAJ92JF8eJwidKf1hIjyH4I EOw6lqJACdF6Zo
kg2rWRjunj4UgK51HTPVyIk=
=4Mjk
-----END PGP SIGNATURE-----
  Réponse avec citation
Vieux 01/07/2008, 09h41   #7
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

fgh.vbn.rty@gmail.com wrote:

> On Jul 1, 2:36 am, rep_movsd <rep.mo...@gmail.com> wrote:
>> Can you explain what the maps represent?
>>
>> That would allow us to make sensible suggestions...
>>
>> Vivek

>
> Well, each index/key represents some property. For instance,
> map<int, map<string, vector<int> > > m1;
>
> The first int key can be something like an enum of a color type (dark,
> light, etc.,) and the second string key may be the color name. So if I
> wanted "dark green integers" I would need m1[DARK]["green"].
>
> In general the indexes are independent of each other.


Magic typing is about just as bad as using magic numbers. You might want to
consider using typedefs:

typedef int color_shade;
typedef string color_name;
...

and then

map< color_shade, map< color_name, ... > >


Best

Kai-Uwe Bux



  Réponse avec citation
Vieux 01/07/2008, 11h13   #8
tharinda_g@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Multiple index maps

On Jul 1, 1:24pm, fgh.vbn....@gmail.com wrote:
> On Jul 1, 2:37 am, tharind...@yahoo.com wrote:
>
>
>
> > On Jul 1, 12:24 pm, fgh.vbn....@gmail.com wrote:

>
> > > I am frequently using maps like the following:
> > > map<string, map<int, vector< pair<int, int> > > > m1;
> > > map<int, map<string, map<int, int> > > m2;

>
> > > This can be a little difficult to maintain if another coder doesn't
> > > really know the what the indexes stand for, the ordering of the
> > > indexes, etc.,

>
> > > Is there a better way to define multiple index maps? Of course, I can
> > > typedef the inner maps into something more readable but is that always
> > > good?

>
> > > Sometimes the index ordering matters and sometimes it doesn't. Does
> > > that make a difference?

>
> > why can't you use a struct to contain the multiple keys and use a
> > compare functor to compare the compound key.

>
> Can you show me an example on how to construct the struct and the
> compound key? Thanks.


#include <map>
#include <string>
#include <iostream>

typedef int color_shade;
typedef std::string color_name;

enum COLOR_SHADES
{
CS_DARK,
CS_LIGHT
};

class CompoundKey
{
public:
struct Compare //This is what I called a functor
{
bool operator()(CompoundKey Left, CompoundKey Right)
{
int iDiff = Left.cs_Shade - Right.cs_Shade;

if(iDiff != 0)
{
return iDiff < 0; //to sort ascending, > 0 to sort descending
}

return strcmp(Left.cn_Name.c_str(), Right.cn_Name.c_str()) < 0;
}
};

CompoundKey(int iKey1, const char* zKey2)
: cs_Shade(iKey1), cn_Name(zKey2)
{
}

CompoundKey(const CompoundKey& rKey)
:cs_Shade(rKey.cs_Shade), cn_Name(rKey.cn_Name) //Copy constructor
to make sure things are copied correctly
{
}

CompoundKey& operator=(CompoundKey& rKey) //Overload the assignment
so the things are copied correctly
{
cs_Shade = rKey.cs_Shade;
cn_Name = rKey.cn_Name;

return *this;
}

~CompoundKey()
{
}

color_shade cs_Shade;
color_name cn_Name;
};

typedef std::map<CompoundKey, int, CompoundKey::Compare> COLOR_MAP;

int main(int argc, char* argv[])
{
COLOR_MAP mapTest;

mapTest[CompoundKey(CS_DARK, "Blue")] = 0xFF0000;
mapTest[CompoundKey(CS_LIGHT, "Red")] = 0xFF;

COLOR_MAP::iterator ite = mapTest.begin();

for (; ite != mapTest.end() ; ++ite)
{
std::cout << "Shade :" << ite->first.cs_Shade
<< ", Name : " << ite->first.cn_Name
<< ", Value : " << ite->second << std::endl;
}

return 0;
}

This is just an example for what I said. But this will be more useful
if you replace the compound key with some class with a color shade,
name and also the value. Also there is an an alternative for the
functor in this instance. Just overload the operator< of the compound
key with the same implementation inside.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h25.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17384 seconds with 16 queries