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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:04:26+00:00 2026-06-17T07:04:26+00:00

for_each(ivec.begin(),ivec.end(), []( int& a)->void{ a = a < 0 ? -a : a; });

  • 0
for_each(ivec.begin(),ivec.end(),
        []( int& a)->void{ a = a < 0 ? -a : a; 
    });

transform(ivec.begin(),ivec.end(),ivec.begin(),
        [](int a){return a < 0 ? -a : a;
    });

I am currently learning lambdas and I am curious how the two implementations, that I have posted above, differ?

  • 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-17T07:04:27+00:00Added an answer on June 17, 2026 at 7:04 am

    This first version:

    for_each(ivec.begin(),ivec.end(),
          []( int& a)->void{ a = a < 0 ? -a : a; 
    });
    

    works by calling the lambda function

     []( int& a)->void{ a = a < 0 ? -a : a; }
    

    once for every element in the range, passing in the elements in the range as arguments. Accordingly, it updates the elements in-place by directly changing their values.

    This second version:

    transform(ivec.begin(),ivec.end(),ivec.begin(),
        [](int a){return a < 0 ? -a : a;
    });
    

    works by applying the lambda function

    [](int a){return a < 0 ? -a : a;}
    

    to each of the elements in the range ivec.begin() to ivec.end(), generating a series of values, and then writing those values back to the range starting at ivec.begin(). This means that it overwrites the original contents of the range with the range of values produced by applying the function to each array element, so the elements are overwritten rather than modified in-place. The net effect is the same as the original for_each, though.

    Hope this helps!

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

Sidebar

Related Questions

For a std::for_each call, is it legal to have both iterators be the end
For example: v.for_each([](int i) { printf(%d\n, i); }); if far more elegant and readable
Why does the for_each call on functor doesn't update sum::total at the end? struct
for each (std::string s in m_intro.text) // std::vector<string> { for (int i = 0;
For each item I have two fields (these values): name (for example, iPhone) and
For each identifier , how can I return the quantity when the received country
For each of the int's in a list (mylist) I would like the following
For each student A who likes a student B where the two are not
For each row of my dataframe, I am currently trying to select all the
For Each c1 In ActiveSheet.UsedRange.SpecialCells(xlCellTypeVisible) If cl.Interior.ColorIndex = 16 Then MsgBox Error in &

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.