Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:28:33+00:00 2026-06-18T14:28:33+00:00

std::vector<std::string> vec1, vec2, vec3, vec4; //populate all vectors, all have the same size //vec1

  • 0
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++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:28:35+00:00Added an answer on June 18, 2026 at 2:28 pm

    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), or std::set and std::multiset (logarithmic lookup and insertion/removal time, the second one with duplicates allowed) instead of a vector.

    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 as std::find or std::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 the vectors might become troublesome.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following: std::string TestFragmentation() { std::vector<char> buffer(500); SomeCApiFunction( &buffer[0], buffer.size() );
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
The Problem Consider the following vectors: std::vector<std::string> extensions; extensions.push_back(.cpp); extensions.push_back(.CPP); extensions.push_back(.h); extensions.push_back(.H); std::vector<std::string> caselessUniqueExtensions;
In my current setup, I have a typedef std::function<void (MyClass&, std::vector<std::string>) MyFunction; std::map<std::string, MyFunction>
I have a std::vector<std::string> which would contain numbers and characters (single char). I would
void insert_string( std::vector<std::string> & strings, const std::string &s ) { std::vector<std::string>::iterator it=lower_bound(strings.begin(),strings.end(),s); if(strings.size()>0) std::cout<<*it<<
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
I have a std::vector<std::string> temp_results and I wish to use std::for_each to go through
I have a simple method: std::vector<string> start() { std::vector<std::string> deletedFiles; // << error appeared
I have a class with the following structure: class Nginx_sender { private: std::vector<std::string> mMessagesBuffer;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.