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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:31:09+00:00 2026-05-13T05:31:09+00:00

I am learning the boost::lambda library and for that I wrote this sample code

  • 0

I am learning the boost::lambda library and for that I wrote this sample code to convert an vector<A> into vector<int> by extracting the value from A object.

class A
{
public:
    A(int n) : m_n(n){}
    int get() const {return m_n;}

private:
    int m_n;

};

int _tmain(int argc, _TCHAR* argv[])
{
    using namespace boost::lambda;

    std::vector<A> a1;
    std::vector<int> a2;
    a1.push_back(A(10));
    a1.push_back(A(20));

    std::for_each(a1.begin(), a1.end(), bind(&std::vector<int>::push_back, var(a2), bind(&A::get, _1)));

    return 0;
}

I could get the for_each part to work after several tries. But I still don’t look the like of it with those multiple binds. Is there any other way to write this. Preferably I would like to do something like: a2.push_back(bind(&A::get,_1));, but that doesn’t compile.

  • 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-13T05:31:10+00:00Added an answer on May 13, 2026 at 5:31 am

    I don’t think what you’re trying to do is possible. for_each applies function to each element in the range, but you are trying to call two member functions and you’ll have to bind them both. I think your code is the best you can do when using for_each. Lambda documentation looks very complete to me, but there is no example for what you’re trying to do. I think it’s for a reason, there is a more appropriate algorithm for this. As Kirill said it’s transform.

    std::transform(a1.begin(), a1.end(), std::back_inserter(a2), bind(&A::get, _1));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 511k
  • Answers 511k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Both approaches sound valid, but if you can design your… May 16, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer You need to split your lines using .split("\n") function, with… May 16, 2026 at 5:15 pm
  • Editorial Team
    Editorial Team added an answer I go back and forth with stuff like this. Before… May 16, 2026 at 5:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am learning boost/asio ad wrote 2 programs(client and server) from e-book with minor
I'm learning how to use the Boost Test Library at the moment, and I
I've just started learning about thread safety. This is making me code a lot
Learning boost, and compiled their daytime server client example . Since I cant use
I'm learning Boost and am having trouble with my makes files. Here is my
I am learning boost/asio and writing example program which was in e-book. of course
In learning Core Animation, I learned very quickly that if you don't do it
Still learning lxml. I discovered that sometimes I cannot get to the text of
Still learning JSP Web Applications here. I have been doing this for a while
I`m learning programming languages. And I decide that I need to lear a new

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.