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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:04:05+00:00 2026-06-03T04:04:05+00:00

The lambda notation has made stl algorithms more accessible. I am still learning to

  • 0

The lambda notation has made stl algorithms more accessible. I am still learning to decide when it’s useful and when to fall back to good old fashioned for-loops.
Often, it becomes necessary to iterate over two (or more) containers of the same size, such that corresponding elements are related, but for some reason are not packed into the same class.

A function using a for-loop to achieve that would look like this:

template<typename Data, typename Property>
void foo(vector<Data>& data, vector<Property>& prop) {
    auto i_data = begin(data);
    auto i_prop = begin(prop);
    for (; i_data != data.end(); ++i_data, ++i_prop) {
        if (i_prop->SomePropertySatistfied()) {
            i_data->DoSomething();
        }
    }
}

In order to use for_each, I need a version of it that handles multiple ranges; something like:

template<typename InputIter1, typename InputIter2, typename Function>
Function for_each_on_two_ranges(InputIter1 first1, InputIter1 last1, InputIter2 first2, Function f) {
    for (; first1 != last1; ++first1, ++first2) {
        f(*first1, *first2);
    }
    return f;
}

With this version, the above code would look like this:

template<typename Data, typename Property>
void foo_two_ranges(vector<Data>& data, vector<Property>& prop) {
    for_each_on_two_ranges(begin(data), end(data), begin(prop), [](Data& d, Property& p) {
        if (p.SomePropertySatistfied()) {
            d.DoSomething();
        }
    });
}

Is there an equivalent way of achieving the same result using stl algorithms?

EDIT

I found the exact answer to my question in the form of boost::for_each running on boost::range. I added the answer, with example code for the sake of completeness.

  • 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-03T04:04:06+00:00Added an answer on June 3, 2026 at 4:04 am

    1) The algorithms in the STL are not meant to cover every possible case, if you need for_each_on_two_ranges then write it (as you have) and use it. The beauty of the STL is it’s so extensible, and you’ve extended it with a useful new algo.

    2) If that doesn’t work, you don’t have to use good old fashioned for-loops, you can use fancy new for-loops instead!

    As another answer said, boost::zip_iterator is your friend here, but it doesn’t have to be hard to use. Here’s a solution using a range adaptor that is implemented with zip_iterator

    template<typename Data, typename Property>
    void foo(vector<Data>& data, vector<Property>& prop) {
        for (auto i : redi::zip(data, prop))
            if (i.get<1>().SomePropertySatistfied())
                i.get<0>.DoSomething();
    }
    

    That zip function creates an adaptor with begin() and end() members that return a boost::zip_iterator, so the loop variable is a tuple of the elements of each underlying container (and as it’s a variadic template you can do it for any number of containers, so you don’t need to write for_each_for_three_ranges and for_each_for_four_ranges etc.)

    You could also use it with for_each

    auto z = redi::zip(data, prop);
    typedef decltype(z)::iterator::reference reference;
    
    for_each(begin(z), end(z), [](reference i) {
        if (i.get<1>().SomePropertySatistfied()) {
            i.get<0>().DoSomething();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning scheme. I know how to use both lambda and let expressions.
What's the notation for declaring a lambda variable, or function parameter, without the use
I am learning lambda calculus but I cant seem to understand the encoding for
I am learning lambda expression and delegates.While i try to execute the following ,I
I'm trying to use Rhinomocks 3.5 and the new lambda notation to mock some
Where can I find complex LINQ examples made using VB.NET Lambda Expression syntax? During
I am using match-lambda to rewrite certain functions in terms of more basic ones.
Consider the following code segment written in S-expr notation: (lambda (x) (lambda (y) (+
The lambda notation x => x.MyProperty is easily confused, by some humans, with greater
in lambda calculus (λ x. λ y. λ s. λ z. x s (y

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.