If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

STL vectors vs. maps

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.

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.

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.

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: