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

  • Home
  • SEARCH
  • 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 7182243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:43:45+00:00 2026-05-28T17:43:45+00:00

I had trouble searching for potential duplicates because I’m not sure what the correct

  • 0

I had trouble searching for potential duplicates because I’m not sure what the correct terminology is.

If I have many vectors which are already created, how can I loop through them? To make things simple, suppose I have three vectors of strings named "vec_one", "vec_two", "vec_three".

I want to do something like:

for i in ("vec_one", "vec_two", "vec_three") {
    for (vector<string>::const_iterator iter = i.begin(); iter != i.end(); ++iter) {
        //do something with the elements ***and I need to access "i"***, that is, the vector name.
    }
}

This would be the same as writing three different for loops, but would be more readable and in fact I have more than three in my non-simple application.

Note that because I need to access the vector name (see the comment), I can’t just merge them all together and then run one loop.

  • 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-05-28T17:43:46+00:00Added an answer on May 28, 2026 at 5:43 pm

    You could put the the vectors in a vector<std::pair<std::string, std::vector<...>*>:

    std::vector<std::pair<std::string, std::vector<std::string>*> > vectors;
    vectors.emplace_back(std::string("vec_one"), &vec_one); //or push_back(std::make_pair(...)) in C++03
    vectors.emplace_back(std::string("vec_two"), &vec_two); 
    vectors.emplace_back(std::string("vec_three"), &vec_three); 
    for(auto iter = vectors.begin(); iter != vectors.end(); ++iter)//used c++11 auto here for brevity, but that isn't necessary if C++11 is not availible
        for(auto vecIter = iter->second->begin(); vecIter != iter->second->end(); ++vecIter)
        //get name with iter->first, body here
    

    That way you can get the name easily from the outer iterator.

    If you use C++11 you can use std::array instead:

    std::array<std::pair<std::string, std::vector<std::string>*>, 3> vectors =
    {
        std::make_pair(std::string("vec_one"), &vec_one),
        std::make_pair(std::string("vec_two"), &vec_two),
        std::make_pair(std::string("vec_three"), &vec_three)
    };
    

    In C++03 you could use buildin arrays instead, but unless the extra overhead for the vector is a problem for you (unlikely) I don’t see a compelling reason to do so. boost::array is also a noteworthy alternative if you can’t use C++11

    If you do need the absolute optimal performance it might be worthwile to directly use const char* instead of std::string for the names.

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

Sidebar

Related Questions

I've been having trouble searching for this answer because I am not quite sure
I had trouble coming up with a title for this question because I'm not
Conceptually, I would like to accomplish the following but have had trouble understand how
Conceptually, I would like to accomplish the following but have had trouble understand how
This question may have been asked before, but I had trouble finding an answer,
This seemed like it should be easy, but I have had trouble getting it
This is possibly answered somewhere already but had trouble searching for it as I
I have had trouble finding help in the matlab documentation and previous questions about
I have had trouble getting web pages to load within Cassini for about a
Any one else had trouble with contracts. I have the following: public void doSomeThing(Stack

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.