Sign in to follow this  

Is this a container type variable that I can access elements by a string of text...

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to figure out if there is a way to access a vector/list/etc via a string of text. For example (the fact I am using vector means nothing)... vector <Something> test; test.pushback(something); Now to access an element within that vector I would need to use a number as an index like... test[1] but instead I would like something that I can access via using a string as the index. I.e. test[zap] Hope that makes sense. I was thinking maybe some kind of map arragment but I am not sure if the overhead outweighs the ease of use benifit. Regards Chad

Share this post


Link to post
Share on other sites
You're looking for something called a dictionary, (pair/sorted/unique) associative (array/container), or a map.

To check out would be std::map or std::multi_map (the former is a unique associative container, meaning that each key is unique).

What are the strings: for example, do they all start with a common prefix?

[Edited by - TheUnbeliever on January 5, 2008 5:26:43 PM]

Share this post


Link to post
Share on other sites
I'd second the suggestion of a std::map<std::string, value_type>.

However, if you need to do a lot of lookups and performance of the string comparisons is an issue, you can introduce an intermediate map that assigns each unique string an integer ID, and then do the map lookup based on that ID.

This is some extra complication though and really isn't worth it unless you've profiled your running code and know for a fact that the performance is an issue. Generally speaking, don't worry too much about performance until you see the program running too slow.

Share this post


Link to post
Share on other sites

This topic is 3627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this