Re: unique value for map
Ashish wrote:
I have a tree/link list which structure is like
struct tree
{
int id;
char *name;
std::map<> *mapobject;
struct tree *left;
struct tree * right;
struct tree * child;
};
This doesn't compile, as the template parameters to std::map are missing.
Also, why are you using a 'char*' instead of a std::string? And why a
pointer to a map object instead of a map object? And why the
repeated 'struct' here? In C++, you don't need that, as opposed to C.
struct tree * treeobject;
If i allocate memory to treeobject
treeobject = malloc(sizeof(struct tree));
Oh dear %DEITY% no! Please stop using malloc(), as it won't call any
constructor. Worse, this doesn't even compile because malloc() returns a
void pointer and you are assigning it to a tree pointer. However, even
using new, you would be faced with the task of properly releasing memory,
so I'd rather not use any dynamic allocation at all or at least use smart
pointers.
my motive is to insert more than one item in treeobject->mapobject.
That object is (if I guess from your above broken code correctly) a pointer.
You cannot insert into a pointer.
I think we can use map pointer in "struct tree" and allocate as much
data as we want.
A std::map (or, basically any C++ object) doesn't care where it is stored,
its behaviour doesn't change with the surrounding entity or the way it is
allocated.
Ashish, I'm afraid that your understanding of C++ is extremely bad and mixed
with some (possibly equally bad) understanding of C. You must learn some
basics first, and for that you need a good book or tutorial. Note that C
and C++ are very different in their use.
Also, you are not supplying the relevant information. Who cares about
the 'id' or 'name' fields of above struct? You maybe, but for asking a
question, that information is best left out, unless it is necessary.
Lastly, you can freely mix any of the standard containers with each other.
All containers are also valid values for a container, because they are
freely copyable and assignable. So yes, you can create a list of maps or a
map of lists.
Uli
--
Sator Laser GmbH
Gesch??ftsf??hrer: Thorsten F??cking, Amtsgericht Hamburg HR B62 932