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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:48:16+00:00 2026-05-28T20:48:16+00:00

Sorry for a little bit beginner question. There are vector and vector of pairs

  • 0

Sorry for a little bit beginner question. There are vector and vector of pairs

typedef std::vector <int> TItems;
typedef std::vector < std::pair <int, int> > TPairs;

Is there any way to transform all first items in pair to another vector in one step

int main ()
{
TItems items;
TPairs pairs;

pairs.push_back (std::make_pair(1,3));
pairs.push_back (std::make_pair(5,7));

std::transform( items.begin(), items.end(), items.begin(), comp ( &pairs ) );

return 0;
}

How to design a functor?

class comp
{
private:
     TPairs *pairs;

public:
    comp ( TPairs  *pairs_ ) : pairs ( pairs_) { }

    unsigned int operator () ( const unsigned int index ) const
    {
        return  (*pairs)[index].second != pairs->end();  //Bad idea
    }
};

Maybe there is some more user friendly method without lambda expressions and loops. Thanks for your help.

  • 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-28T20:48:16+00:00Added an answer on May 28, 2026 at 8:48 pm

    First of all, you should use a back_inserter as the third argument to transform so that the transformed values are pushed to the back of the vector.

    Second, you need some sort of functor which takes a pair of ints and returns the first one. This should do:

    int firstElement( const std::pair<int, int> &p ) {
        return p.first;
    }
    

    Now, to put the pieces together:

    TPairs pairs;
    pairs.push_back( std::make_pair( 1, 3 ) );
    pairs.push_back( std::make_pair( 5, 7 ) );
    
    TItems items;
    std::transform( pairs.begin(), pairs.end(), std::back_inserter( items ),
                    firstElement );
    

    After this code, items contains 1 and 5.

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

Sidebar

Related Questions

Sorry for posting this question again. I rephrased my question a little bit. I
My question title sounds a little bit difficult - sorry. I'm new in MEF
I know this has a stupid solution but, sorry, I'm little bit confused. I
Sorry if this question sounds a little silly, but I am not sure what
This question will be a little longer and I am sorry for that :)
Sorry if the title of the question is a little misleading but I was
Sorry the title is a little bit long. So I have a twitter account
Guys sorry if sounds a little bit naive... In an activity I have one
**Edit:**I am sorry, but the situation could be a little bit more complex than
I need a little bit of help with using pointers in C++. Sorry to

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.