Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I have a record like this:
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 31-Oct-11 4:15am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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).
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
CPallini at 31-Oct-11 10:58am
   
I second the two maps idea.
Richard MacCutchan at 31-Oct-11 11:01am
   
Thanks, it was the only thing I could think of!
Joseph Marzbany at 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)

  Print Answers RSS
0 OriginalGriff 515
1 Maciej Los 340
2 Richard MacCutchan 265
3 BillWoodruff 225
4 Mathew Soji 155
0 OriginalGriff 8,804
1 Sergey Alexandrovich Kryukov 7,457
2 DamithSL 5,689
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411028.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100