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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:28:41+00:00 2026-05-19T13:28:41+00:00

This is more of a learning question. Is there a way I can write

  • 0

This is more of a learning question. Is there a way I can write the following for-loop using std::for_each or std::transform? If not, is there anything in boost that can help on this? The loop simply flattens a vector of vectors into one long vector.

vector<vector<int> > int_vectors;

// ... fill int_vectors

vector<int> ints;

for (vector<vector<int> >::const_iterator iter = int_vectors.begin(); iter != int_vectors.end(); ++iter) {
   ints.insert(ints.end(), iter->begin(), iter->end());
}
  • 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-19T13:28:42+00:00Added an answer on May 19, 2026 at 1:28 pm

    I wouldn’t change this to use one of the algorithms unless you have a compiler that supports lambdas. It’s completely clear as written. Even if your compiler did support lambdas, I’d probably not change this code.

    One relatively straightforward option would be to write a flattening iterator. I wrote one for demonstration in an answer to another question.

    If you really want a one-liner and can use bind (boost::bind from Boost, std::tr1::bind from TR1, and std::bind from C++0x will all work), then here is how that would look. I warn you in advance: it is horrible.

    Edit: Technically this is also illegal. The type of a Standard Library member function is unspecified, so you cannot (portably or correctly) take the address of such a member function. If you could correctly take the address of a Standard Library member function, this is what it would look like:

    typedef std::vector<int>::iterator (std::vector<int>::*IteratorGetter)();
    
    std::for_each(int_vectors.begin(), int_vectors.end(),
        std::bind(
            std::bind(
                &std::vector<int>::insert<std::vector<int>::iterator>, 
                &ints, 
                std::bind((IteratorGetter)&std::vector<int>::end, &ints), 
                _1, 
                _2
            ),
            std::bind((IteratorGetter)&std::vector<int>::begin, _1),
            std::bind((IteratorGetter)&std::vector<int>::end, _1)
        )
    );
    

    (Yes, that is technically one “line of code” as it is a single statement. The only thing I have extracted is a typedef for the pointer-to-member-function type used to disambiguate the overloaded begin and end functions; you don’t necessarily have to typedef this, but the code requires horizontal scrolling on Stack Overflow if I don’t.)

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

Sidebar

Related Questions

I am wondering if there is a way to handle this more elegantly. After
This is more of an academic inquiry than a practical question. Are there any
This is more of a syntax question I'm trying to write a store procedure
This is more of a business-oriented programming question that I can't seem to figure
Ok, I'm going to try to make this more clear because my last question
This is more of an generic XML Schema question, but if and how do
This is more an observation than a real question: MS-Access (and VBA in general)
I asked a still unanswered question that will shed more light on this question.
IS there a way to post a $var to $PHP_SELF so that I can
Could you guys please tell me how I can make the following code more

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.