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 > Initialising a map
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Initialising a map

Réponse
 
LinkBack Outils de la discussion
Vieux 27/06/2008, 14h06   #1
Angus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Initialising a map

Should I be able to do something like this:

static const std::map<RequestType, string> RequestMap = {
{ AOpenEx, "AOpenEx" },
{ AClose, "AClose" },
{ ARegister, "ARegister" },
{ AUnregister, "Unregister" } };

RequestType is an enum.

I can't compile it. Or can I not initialise with an array like this?
  Réponse avec citation
Vieux 27/06/2008, 14h13   #2
David Côme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscomber@gmail.com> wrote:

> Should I be able to do something like this:
>
> static const std::map<RequestType, string> RequestMap = {
> { AOpenEx, "AOpenEx" },
> { AClose, "AClose" },
> { ARegister, "ARegister" },
> { AUnregister, "Unregister" } };
>
> RequestType is an enum.
>
> I can't compile it. Or can I not initialise with an array like this?



look at boost::assign and particularly at map_list_of
  Réponse avec citation
Vieux 27/06/2008, 14h30   #3
Angus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

On 27 Jun, 14:13, David Côme <davidc...@wanadoo.fr> wrote:
> On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscom...@gmail.com> wrote:
> > Should I be able to do something like this:

>
> > static const std::map<RequestType, string> RequestMap = {
> > { AOpenEx, "AOpenEx" },
> > { AClose, "AClose" },
> > { ARegister, "ARegister" },
> > { AUnregister, "Unregister" } };

>
> > RequestType is an enum.

>
> > I can't compile it. Or can I not initialise with an array like this?

>
> look at boost::assign and particularly at map_list_of


Unfortunately I cannot use boost here. Maybe it is easier not to use
a map as the data is fairly static. What is the easiest way for me to
create a mapping like this?
  Réponse avec citation
Vieux 27/06/2008, 14h44   #4
rep_movsd
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

On Jun 27, 4:30pm, Angus <anguscom...@gmail.com> wrote:
> On 27 Jun, 14:13, David Côme <davidc...@wanadoo.fr> wrote:
>
> > On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscom...@gmail.com> wrote:
> > > Should I be able to do something like this:

>
> > > static const std::map<RequestType, string> RequestMap = {
> > > { AOpenEx, "AOpenEx" },
> > > { AClose, "AClose" },
> > > { ARegister, "ARegister" },
> > > { AUnregister, "Unregister" } };

>
> > > RequestType is an enum.

>
> > > I can't compile it. Or can I not initialise with an array like this?

>
> > look at boost::assign and particularly at map_list_of

>
> Unfortunately I cannot use boost here. Maybe it is easier not to use
> a map as the data is fairly static. What is the easiest way for me to
> create a mapping like this?


Maybe use a loop?

RequestType keys[] = {AOpenEx, AClose, ARegister, AUnregister,};
const char* vals[] = {"AOpenEx", "AClose", "ARegister",
"AUnregister"};

std::map<RequestType, string> myMap;
for(int n = 0; n < (sizeof(keys) / sizeof(keys[0])); ++n)
{
myMap[keys[n]] = vals[n];
}

....

If you are always going to have the string exactly the same as
RequestType enum and you have a large number of such, maybe you can do
some preprocessor stuff, this will avoid any errors while typing.

Vivek


  Réponse avec citation
Vieux 27/06/2008, 15h04   #5
SeanW
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

On Jun 27, 9:30 am, Angus <anguscom...@gmail.com> wrote:
> On 27 Jun, 14:13, David Côme <davidc...@wanadoo.fr> wrote:
>
> > On Fri, 27 Jun 2008 15:06:09 +0200, Angus <anguscom...@gmail.com> wrote:
> > > Should I be able to do something like this:

>
> > > static const std::map<RequestType, string> RequestMap = {
> > > { AOpenEx, "AOpenEx" },
> > > { AClose, "AClose" },
> > > { ARegister, "ARegister" },
> > > { AUnregister, "Unregister" } };

>
> > > RequestType is an enum.

>
> > > I can't compile it. Or can I not initialise with an array like this?

>
> > look at boost::assign and particularly at map_list_of

>
> Unfortunately I cannot use boost here. Maybe it is easier not to use
> a map as the data is fairly static. What is the easiest way for me to
> create a mapping like this?


How about:

typedef std::map<RequestType, string> RequestMap;
RequestMap tmp_map;
tmp_map[AOpenEx] = "AOpenEx";
tmp_map[AClose] = "AClose";
tmp_map[ARegister] = "ARegister";
tmp_map[AUnregister] = "AUnregister";
static const RequestMap const_map(tmp_map);

A bit ugly, but it works.

You might also be interested in this proposal for future
C++ initializer lists:

http://anubis.dkuug.dk/jtc1/sc22/wg2...2003/n1509.pdf


Sean
  Réponse avec citation
Vieux 27/06/2008, 15h30   #6
AnonMail2005@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

On Jun 27, 9:06am, Angus <anguscom...@gmail.com> wrote:
> Should I be able to do something like this:
>
> static const std::map<RequestType, string> RequestMap = {
> { AOpenEx, "AOpenEx" },
> { AClose, "AClose" },
> { ARegister, "ARegister" },
> { AUnregister, "Unregister" } };
>
> RequestType is an enum.
>
> I can't compile it. Or can I not initialise with an array like this?

If your map contents won't change much in the future, just initialize
it
from two arrays or an array of POD structs.

If the map will be added to in the future, you may want to look into
having a register function. See Modern C++ Design for how to do this.
In that book, he used this technique to register names/creation
function pointers with a factory.

HTH
  Réponse avec citation
Vieux 27/06/2008, 16h31   #7
tragomaskhalos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

On Jun 27, 2:06pm, Angus <anguscom...@gmail.com> wrote:
> Should I be able to do something like this:
>
> static const std::map<RequestType, string> RequestMap = {
> { AOpenEx, "AOpenEx" },
> { AClose, "AClose" },
> { ARegister, "ARegister" },
> { AUnregister, "Unregister" } };
>
> RequestType is an enum.


Check this out (a previous thread on the same question):
http://groups.google.com/group/comp....a984af51eba51a

  Réponse avec citation
Vieux 27/06/2008, 18h40   #8
Juha Nieminen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Initialising a map

Angus wrote:
> static const std::map<RequestType, string> RequestMap = {
> { AOpenEx, "AOpenEx" },
> { AClose, "AClose" },
> { ARegister, "ARegister" },
> { AUnregister, "Unregister" } };


Followup question: Will this be possible with the upcoming C++
standard? (AFAIK something similar will be possible with std::vector.)
  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 11h10.


Édité par : vBulletin® version 3.7.2
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,89120 seconds with 16 queries