std::vector<std::string> vec1, vec2, vec3, vec4;
//populate all vectors, all have the same size
//vec1 has different values
Now given some “key” in vec1, such as “foo”, how do I quickly get the corresponding strings from the other vectors?
I will have to do this many times, with different keys in vec1, so this operation has to be fast.
Should I create a map that maps the elements in vec1 into index values (0,1,2,3,4…)?
How is this best done in C++?
Depends on what you mean by “quickly”.
If you care about complexity of retrieval by value, I would suggest to consider using an associative container such as
std::unordered_set(constant lookup and insertion/removal time), orstd::setandstd::multiset(logarithmic lookup and insertion/removal time, the second one with duplicates allowed) instead of avector.However, it must be said that
vectors allocate a contiguous region of memory to store their elements, so linear access will result in a high cache hit rate: thus, even though the complexity is worse, access is still “fast” in general, and you can use regular STL algorithms such asstd::findorstd::find_if()to find elements that match a given value or satisfy a given predicate.Often, locality of the data can compensate for a worse complexity. The key here is to always do repeated measurements to determine what’s the solution which gives you the best performance.
That said, the optimal solution is likely to depend on your overall workload: are you doing element-by-element iterations of your vectors at all? How often do you need to retrieve your elements by position? If those are not frequent operations, you might not need a vector. Moreover, how often are those vectors updated? How often do you need to lookup an element in those vectors by value? Your question does not say much about this.
If memory overhead is not an issue for you, you can certainly consider building a separate map as an index, and maintain redundant structures. If your
vectors will be frequently updated with insertions and deletions, however, ensuring the consistency of the index and thevectors might become troublesome.