Click here to Skip to main content
11,922,381 members (31,792 online)
Rate this:
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
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).
Rate this: bad
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.
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 Marzbany 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 453
1 Dave Kreskowiak 335
2 Sergey Alexandrovich Kryukov 263
3 Richard MacCutchan 225
4 ppolymorphe 175
0 OriginalGriff 7,231
1 KrunalRohit 4,530
2 Sergey Alexandrovich Kryukov 3,356
3 George Jonsson 2,865
4 Suvendu Shekhar Giri 2,216

Advertise | Privacy | Mobile
Web02 | 2.8.151120.1 | Last Updated 31 Oct 2011
Copyright © CodeProject, 1999-2015
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