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 8714201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:32:15+00:00 2026-06-13T05:32:15+00:00

i would like to iterate through a vector and check if elements are vectors

  • 0

i would like to iterate through a vector and check if elements are vectors or strings. Also i need a way to pass different vecors to a function.
Something like this:

using namespace std;
string toCustomString(<some vector> vec) {
    string ret = "";
    for(size_t i = 0; i < vec.length(); ++i) 
        if (vec[i] == %vector%)
            ret += toCustomString(vec[i]);
        else //if type of vec[i] is string
            ret += "foo"+vec[i]+"bar";
    }
    return ret;
}
  • Well, first i need to know how i can check correctly if vec[i] is a std::vector

  • Then i need to know how to define the paramater for the function to accept any kind of (multidimensional) vector

  • 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-13T05:32:16+00:00Added an answer on June 13, 2026 at 5:32 am

    std::vector can only contain one type – that is the T in std::vector<T>, which can be accessed with the member value_type.

    What you probably are looking for is template specialization:

    template<typename T>
    string toCustomString(std::vector<T> vec) {
        // general case
    }
    
    template<>
    string toCustomString<std::string>(std::vector<std::string> vec) {
        // strings
    }
    

    (if you want to partially specialize it over all vectors then you’ll need to lift it to a struct)

    If you really want to store both strings and vectors in the vector then look at Boost.Variant and Boost.Any

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

Sidebar

Related Questions

I would like to know how to iterate through all the elements in a
I would like to be able to iterate through the grid elements with a
I would like multiple threads to iterate through the elements in a LinkedList. I
I would like to iterate through all elements in an std::list in parallel fashion
I would like to iterate through an array that covers every pixel on my
Here's the situation: I would like to iterate through a table with input controls,
When 'login' button is clicked I would like to iterate through a column in
I would like to randomly iterate through a range. Each value will be visited
Does map() iterate through the list like for would? Is there a value in
I would like to iterate over a collection and at the same time also

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.