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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:13:13+00:00 2026-06-18T02:13:13+00:00

I have been trying to fill a vector from a map. I know how

  • 0

I have been trying to fill a vector from a map.
I know how to do this in a more conventional way, but I was trying to achive it with STL algorithms (a one liner) as some kind of a training :).

the origin map type is :

std::map< std::string, boost::shared_ptr< Element > >

the destination vector is :

std::vector< Element > theVector;

what I have so far is this:

std::transform( theMap.begin(), theMap.end(),
        std::back_inserter( theVector ),
        boost::bind( &map_type::value_type::second_type::get, _1 )
        );

But this is trying to insert a pointer in the vector which doesn’t work.
I have also tried this:

using namespace boost::lambda;
using boost::lambda::_1;

std::transform( theMap.begin(), theMap.end(),
        std::back_inserter( theVector ),
        boost::bind( &map_type::value_type::second_type::get, *_1 )
        );

But it’s not working either.

Edit:

I’ve got this working solution but I find it less impressive 🙂

std::for_each( theMap.begin(), theMap.end(), 
        [&](map_type::value_type& pair)
        {
            theVector.push_back( *pair.second );
        } );

Edit2:
The thing I’m the less comfortable with here is bind(), so bind() solutions are welcome!

  • 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-18T02:13:14+00:00Added an answer on June 18, 2026 at 2:13 am

    Another alternative may be the new for syntax:

    for(auto &cur_pair: the_map) { theVector.push_back(*(cur_pair.second)); }
    

    It’s at least a one-liner (kinda), though it’s just another way to do your std::for_each but more compact.

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

Sidebar

Related Questions

I have been trying to extract data from a database and fill in a
I have been trying to work out the bug on this but can't seem
This seems like such a simple question, but I have been trying for an
I know this question was answered, in C# and I have been trying to
I have been trying to fill an array in Xcode with xml output from
I have been trying to get it to work using this thread but can't
I have been trying to figure this problem out without asking for help, but
I have been trying to figure this out but it seems no matter how
I have been trying to do this for a while now but i can
I have been trying this for a while now and can't get it 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.