how to build a recursive map ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jose luis fernandez diaz

    how to build a recursive map ?

    Hi,

    When I want build a tree I define the structure below:

    struct node
    {
    string data;
    node *leaf;
    node *right;
    };


    I want to define a map where each element is a map. Something similar to:

    map<string, map *> m1;


    Any idea ?

    Thanks,
    Jose Luis.
  • Karl Heinz Buchegger

    #2
    Re: how to build a recursive map ?

    jose luis fernandez diaz wrote:[color=blue]
    >
    > I want to define a map where each element is a map. Something similar to:
    >
    > map<string, map *> m1;[/color]

    This is not a map of maps, it is a map of pointers to maps.
    Anyway: what is your problem?

    lets say you have a map, which maps int to double

    map< int, double >

    you now want to build a map out of this, such that a string
    selects the map which maps int to double.

    map< string, map< int, double > >

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • John Harrison

      #3
      Re: how to build a recursive map ?


      "jose luis fernandez diaz" <jose_luis_fdez _diaz_news@yaho o.es> wrote in
      message news:c2f95fd0.0 404200623.77eb4 25f@posting.goo gle.com...[color=blue]
      > Hi,
      >
      > When I want build a tree I define the structure below:
      >
      > struct node
      > {
      > string data;
      > node *leaf;
      > node *right;
      > };
      >
      >
      > I want to define a map where each element is a map. Something similar to:
      >
      > map<string, map *> m1;
      >
      >
      > Any idea ?
      >
      > Thanks,
      > Jose Luis.[/color]

      Like this

      class Recursive
      {
      public:
      ...
      private:
      map<string, Recursive*> m1;
      };

      john


      Comment

      • jose luis fernandez diaz

        #4
        Re: how to build a recursive map ?

        Karl Heinz Buchegger <kbuchegg@gasca d.at> wrote in message news:<40853AAB. 18C8762@gascad. at>...[color=blue]
        > jose luis fernandez diaz wrote:[color=green]
        > >
        > > I want to define a map where each element is a map. Something similar to:
        > >
        > > map<string, map *> m1;[/color]
        >
        > This is not a map of maps, it is a map of pointers to maps.
        > Anyway: what is your problem?
        >
        > lets say you have a map, which maps int to double
        >
        > map< int, double >
        >
        > you now want to build a map out of this, such that a string
        > selects the map which maps int to double.
        >
        > map< string, map< int, double > >[/color]

        I need something like this:

        template<class key, class T, . . .>
        class map
        {
        typedef Key key_type;
        // typedef T mapped_type;
        typedef map *mapped_type;

        . . .
        }

        map<string> m1;
        m1["one"] = NULL;
        m2["two"] = m1;

        but I think that it is not posible.

        I tried to simulate it with:

        map<string, map<string, map<string ... string> . . . > m_n;

        m_1["one"] = "zero";
        m_2["two"] = m1["one];
        .. . .
        m_n["n"] = n_n_1["n-1"];


        but this is ugly.

        Regards,
        Jose Luis.

        Comment

        • Dan Cernat

          #5
          Re: how to build a recursive map ?

          jose_luis_fdez_ diaz_news@yahoo .es (jose luis fernandez diaz) wrote in message news:<c2f95fd0. 0404200623.77eb 425f@posting.go ogle.com>...[color=blue]
          > Hi,
          >
          > When I want build a tree I define the structure below:
          >
          > struct node
          > {
          > string data;
          > node *leaf;
          > node *right;
          > };
          >
          >
          > I want to define a map where each element is a map. Something similar to:
          >
          > map<string, map *> m1;
          >
          >
          > Any idea ?
          >
          > Thanks,
          > Jose Luis.[/color]

          try

          map<string, void*>

          and cast the void* to a map<string, void*> internally and so on. you
          stop when the pointer is null.

          to add elements to such thing:

          map<string, void*> leaf1, leaf2;
          leaf1["aa"] = 0;
          laef2["bb"] = 0;

          map<string, void*> node1;
          node1["leaf1"] = leaf1;
          node1["leaf2"] = leaf2;

          dan

          Comment

          Working...