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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:23:21+00:00 2026-05-14T15:23:21+00:00

I have the following types: struct X { int x; X( int val )

  • 0

I have the following types:

struct X { int x; X( int val ) : x(val) {} };
struct X2 { int x2; X2() : x2() {} };

typedef std::pair<X, X2>      pair_t;
typedef std::vector<pair_t>   pairs_vec_t;
typedef std::vector<X>        X_vec_t;

I need to initialize instance of pairs_vec_t with values from X_vec_t. I use the following code and it works as expected:

int main()
{
  pairs_vec_t ps;
  X_vec_t xs; // this is not empty in the production code

  ps.reserve( xs.size() );

  { // I want to change this block to one line code.
    struct get_pair {
      pair_t operator()( const X& value ) { 
        return std::make_pair( value, X2() ); }
    };
    std::transform( xs.begin(), xs.end(), back_inserter(ps), get_pair() );
  }

  return 0;
}

What I’m trying to do is to reduce my copying block to one line with using boost::bind. This code is not working:

for_each( xs.begin(), xs.end(), boost::bind( &pairs_vec_t::push_back, ps, boost::bind( &std::make_pair, _1, X2() ) ) );

I know why it is not working, but I want to know how to make it working without declaring extra functions and structs?

  • 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-14T15:23:21+00:00Added an answer on May 14, 2026 at 3:23 pm

    something like this?

    using boost::lambda;
    X2 x;
    transform(..., (bind(std::make_pair<X,X2>, _1, ref(x))));
    

    I cant check at the moment, but if recalled correctly from memory, the above is valid.

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

Sidebar

Related Questions

I have some generic types, like the following: public struct Tuple<T1, T2> { ...
I have the following code parts: typedef struct Board* BoardP; typedef struct Board {
I want to accomplish the following scenario in Drupal: You have 2 content-types. Lets
If I have the following code: MyType<int> anInstance = new MyType<int>(); Type type =
I have come across the following type of code many a times, and I
So I have the following user defined type in my oracle database: CREATE OR
I have the following javascript: $.ajax({ type: "POST", dataType: "json", url: "/Home/Submit", data: {
Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
I have the following javascript: css = document.createElement('style'); css.setAttribute('type', 'text/css'); css_data = document.createTextNode(''); css.appendChild(css_data);
In an oscommerce site I have the following: Server response = Content-type: text/html;charset=UTF-8 and

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.