how to build a recursive map ?

Discussion in 'C++' started by jose luis fernandez diaz, Apr 20, 2004.

  1. 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.
     
    jose luis fernandez diaz, Apr 20, 2004
    #1
    1. Advertising

  2. jose luis fernandez diaz wrote:
    >
    > I want to define a map where each element is a map. Something similar to:
    >
    > map<string, map *> m1;


    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
     
    Karl Heinz Buchegger, Apr 20, 2004
    #2
    1. Advertising

  3. "jose luis fernandez diaz" <> wrote in
    message news:...
    > 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.


    Like this

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

    john
     
    John Harrison, Apr 20, 2004
    #3
  4. Karl Heinz Buchegger <> wrote in message news:<>...
    > jose luis fernandez diaz wrote:
    > >
    > > I want to define a map where each element is a map. Something similar to:
    > >
    > > map<string, map *> m1;

    >
    > 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 > >


    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.
     
    jose luis fernandez diaz, Apr 20, 2004
    #4
  5. jose luis fernandez diaz

    Dan Cernat Guest

    (jose luis fernandez diaz) wrote in message news:<>...
    > 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.


    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
     
    Dan Cernat, Apr 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. actuary77
    Replies:
    2
    Views:
    297
    Kent Johnson
    Mar 2, 2005
  2. Replies:
    2
    Views:
    362
  3. so.intech
    Replies:
    4
    Views:
    604
    so.intech
    Aug 9, 2006
  4. n00m
    Replies:
    12
    Views:
    1,115
  5. vamsi
    Replies:
    21
    Views:
    2,081
    Keith Thompson
    Mar 9, 2009
Loading...

Share This Page