std::map keys in C++
- by Soumava
I have a requirement to create two different maps in C++. The Key is of type CHAR * and the Value is a pointer to a struct. I am filling 2 maps with these pairs, in separate iterations. After creating both maps I need find all such instances in which the value of the string referenced by the CHAR * are same.
For this i am using the following code :
typedef struct _STRUCTTYPE
{
..
}
STRUCTTYPE, *PSTRUCTTYPE;
typedef pair {CHAR *,PSTRUCTTYPE} kvpair;
..
CHAR *xyz;
PSTRUCTTYPE abc;
after filling the information;
Map.insert (kvpair(xyz,abc));
the above is repeated x times for the first map, and y times for the second map.
after both are filled out;
std::map {CHAR *, PSTRUCTTYPE} :: iterator Iter,findIter;
for (Iter=iteratedMap-begin();Iter!=iteratedMap-end();mapIterator++)
{
char *key = Iter-first;
printf("%s\n",key);
findIter=otherMap-find(key);
//printf("%u",findIter-second);
if (findIter!=otherMap-end())
{
printf("Match!\n");
}
}
The above code does not show any match, although the list of keys in both maps show obvious matches. My understanding is that the equals operator for CHAR * just equates the memory address of the pointers.
My question is, what should i do to alter the equals operator for this type of key or could I use a different datatype for the string?
*note : {} has been used instead of angle brackets as the content inside angle brackets was not showing up in the post.