Click here to Skip to main content
13,005,198 members (60,564 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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 3: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
Top Experts
Last 24hrsThis month

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