how to build a recursive map ?

  • Thread starter jose luis fernandez diaz
  • Start date
J

jose luis fernandez diaz

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

Karl Heinz Buchegger

jose said:
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 > >
 
J

John Harrison

jose luis fernandez diaz said:
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
 
J

jose luis fernandez diaz

Karl Heinz Buchegger said:
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.
 
D

Dan Cernat

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top