I need a collection which will associate a string with an integer. The strings will be added numerically and so the number associated to the first string added will be 0, the second 1, etc. I was thinking I would use a vector, but the problem is that I then want to be able to search the collection for the string and find the integer related to it. A vector does not seem to have an existing search algorithm (am I wrong?). I could write one myself, but I was hoping there was another way.
I also looked at using a map, but I am finding the interface to a map to be needlessly complex. The example I have looked at shows:
theMap.insert(INT2STRING::value_type(0,"Zero"));
where I would rather just use:
theMap.insert(0,"Zero");
and searching the map involves iterators, and all sorts of other needless garbage when I just want to have a method like:
int find(string searchString);
So do I need to write a custom class or method, or is there something I am not seeing.
thanks,
eggman
January 11th, 2004, 08:56 PM
Kheun
You can try MFC CMap or other CMap-favored template classes.
January 11th, 2004, 09:22 PM
kuphryn
A vector and a map are different containers. Each is a good solution for certain problems.
Kuphryn
January 11th, 2004, 09:32 PM
Philip Nicoletti
More info is needed to determine which container is best.
For example, will there be any "missing" numbers ? Or
will they all be included.
Also, it is not clear whether you want the int or the string to
be the key. Your insert had the int as the key, but the
find example that you gave was trying to lookup by string.
There are no missing integers. They will start at 0 and go up in order 0,1,2,3,4 etc. There are also no duplicates, so it could be a key.
There are no duplicate strings either. Each integer maps to a single string. Each string maps to a single integer. For search purposes, I want to be able to supply a string, and receive an integer in return. I can not see any reason why I would want to do the opposite, so it would seem the String should be the key.
eggman
January 11th, 2004, 10:46 PM
Eggman002
well, I think with the above examples I can use either map or cmap. Maps still seem to have some irritating necessities, like using an iterator, etc, but it does not look as bad as I initially thought. Anyway, that should do for now. Topic closed.
January 12th, 2004, 12:19 AM
Philip Nicoletti
Just another point, I too sometimes prefer I different client
interface that fit special needs better. You can create your
own class with the map as a variable and put whatever
type of inteface you want. Something like this: