Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a record like this:
C++
struct WORD
{
    int iID;
    char* pchWord;
    .
    .
    .
};

if I want to have my records in a map class and explore through them later, I can just have one variable as KEY to insert(), find(), and etc. But some times I like to explore through my records using their ID as well as their "pchWord" value; howwwwww
Posted

Richard has already suggested a very good way to do this. Here's a more C-ish alternative.

Replace the int iID with a void* iID. You can continue to assign int values to it without overflow (on 32 bit and 64 bit targets).

Now just use a std::multimap to store your data. It allows multiple keys to map to the same values.

Now here's the caveat : you have to manually make sure that there are no collisions between iID and pchWord. A simple way to accomplish this is to use char[0] arrays to get the iID values (making them unique addresses in the process memory space).

That said, I'd prefer Richard's suggestion myself. Consider my suggestion to be purely of academic interest (mostly for experimentation).
 
Share this answer
 
v2
You could implement all functionality yourself, or perhaps have two maps, one of which uses the iID as a key to the pchWord or vice versa.
 
Share this answer
 
Comments
CPallini 31-Oct-11 10:58am    
I second the two maps idea.
Richard MacCutchan 31-Oct-11 11:01am    
Thanks, it was the only thing I could think of!
Joseph Marzbani 31-Oct-11 11:06am    
end up with a new class :-) OK guys, thnx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900