Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

nested data structures

Status
Not open for further replies.

DoraC

Programmer
May 7, 2002
98
0
0
US
Hi,

I need to be able to create nested data structures in C++, e.g. maps of maps (maps with values being maps). I'm not sure how to do this...

I've tried:
Code:
#include <map>

map<string, map* > m;
and other such combinations, to no avail. I know this has to do with the fact that map is a template, but I'm not sure how to work around it.

Thank you very much,
dora c
 
You need to write something like this
Code:
map < char*, map<int,int> > mymap;
The outer level maps a string to a map<int,int>
From there, you map one integer to another

Like so
Code:
    mymap["hello"][0] = 1;

--
 
Thank you for your reply,

This leads to the following question...

is it possible to store in a map values of different map-types?.. (e.g. map<int,int>, map<string, string>, etc.)

I think the answer is probably no, but am not sure. Perhaps I've been living in the world of weak-typed scripting languages (and Java) for too long...

thanks, and any help appreciated..
dora c
 
Not sure thats what you mean by "different map-types" but
the key and value of a specific map can only be of one type each (unless you use union - but thats cheating), ie you cant have a map where the value is string and int at the same time. You can however store pointers in a map so you could for example store pointers to some base class that are instantiated as some sub class.

If the value is infact a map, all values are of that same map type.


/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Hi,

I have a map of the form
map<char *, map<bitset<14>,bitset<14>,Lessthanbit > Lessthanchar>

My problem is I can iterate through the first key (char *)
but I am not sure how to iterate through the keys of the second map?? (ie. for a specific char *, iterate through its keys only)

Any reply is appreciated
Thanks,
Bila
 
Something like (typedeffing for clarity):
Code:
typedef map<bitset<14>,bitset<14>,Lessthanbit>  BitSetMap;
typedef map<const char*, BitSetMap, Lessthanchar> StringMap;
...
StringMap theStringMap;
...
StringMap::const_iterator it = theStringMap.find("some string");
if (it!=theStringMap.end())
{
  // Found "some string"
  const BitSetMap& bitsetMap = (*it).second;

  // Iterate its bitset map
  for(BitSetMap::const_iterator bsIt = bitsetMap.begin();bsIt!=bitsetMap.end();++bsIt)
  {
    // (*bsIt).first holds the key, (*bsIt).second holds the value
     ...
  }
}

Btw, I'd think it'd be a good idea to store the string as const char*, rather than just char*.

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Wouldn't it be better to store the string as std::string? That is, if space and time are not a problem in the programming environment. I can understand using char*: if, like me, you've had bad experiences with RWCString and CString in other environments, you'd like to keep to the basics as much as possible.

Not sure what Lessthanchar would do. functor with a strcmp, probably. If you used std::string, then the comparison operators are already defined.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top