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

The Archive Base Latest Questions

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

I have the following code: #include <iostream> #include <vector> #include <algorithm> int main( int

  • 0

I have the following code:

#include <iostream>
#include <vector>
#include <algorithm>

int main( int argc, char* argv[] )
{
    std::vector< int > obj;
    obj.push_back( 10 );
    obj.push_back( 20 );
    obj.push_back( 30 );
    std::for_each( obj.begin(), obj.end(), []( int x )
    { 
        return x + 2; 
    } );
    for( int &v : obj )
        std::cout << v << " ";
    std::cout << std::endl;
    return 0;
}

The result is : 10, 20, 30

i want to change all elements in vector (obj), using Lambda functions of new C++11 standard.

This is the code of implementation for_each function:

template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
    for ( ; first!=last; ++first )
        f(*first);
    return f;
}

*first passed by value and cope of element is changed, what is the alternative of for_each i must use that i have a result: 12, 22, 32 ?

  • 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-26T17:44:44+00:00Added an answer on May 26, 2026 at 5:44 pm

    i want to change all elements in vector (obj), using Lambda functions of new C++11 standard.

    You’ve to do this :

    std::for_each( obj.begin(), obj.end(), [](int & x)
    {                                          //^^^ take argument by reference
       x += 2; 
    });
    

    In your (not my) code, the return type of the lambda is deduced as int, but the return value is ignored as nobody uses it. That is why there is no return statement in my code, and the return type is deduced as void for this code.

    By the way, I find the range-based for loop less verbose than std::for_each for this purpose:

    for( int &v : obj )  v += 2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have following code #include <iostream> using namespace std; int main(int argc,char arg[]){ int
I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int
I have following code #include <iostream> #include <string> #include <algorithm> using namespace std; int
I have the following code #include <iostream> #include <vector> using namespace std; int distance(vector<int>&
i have following code #include <iostream> #include <cstdlib> using namespace std; int main() {
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have the following code: #include <iostream> #include <string> void main() { std::string str;
I have the following code: #include <iostream> using namespace std; struct S { S(int
I have the following code compiled by gcc: #include <iostream> using namespace std; class
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =

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.