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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:03:53+00:00 2026-05-30T05:03:53+00:00

What is the C++ idiomatic way of creating a std::vector from the last n

  • 0

What is the C++ idiomatic way of creating a std::vector from the last n elements of a std::map?

I am not interested in preserving the order in the vector.

I can copy the elements, like this:

    std::map< double, MyType > m;
    size_t n = 3;
    std::vector< MyType > v;
    std::map< double, MyType >::iterator it = m.end();
    while ( n-- ) { // assuming m.size() >= n
        it--;
        v.push_back(it->second);
    }

But, is there any other way, more idiomatic, to do it?

  • 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-30T05:03:55+00:00Added an answer on May 30, 2026 at 5:03 am

    std::copy would be suitable if you wanted to copy the types unchanged. However, std::map<T,U>::iterator_type::value_type is not U (the type you want to copy), but std::pair<T,U> (in other words, dereferencing a map iterator yields a pair of the key and value types), so a raw copy won’t work.

    So we need to copy the elements, performing a transformation along the way. That’s what std::transform is for.

    For convenience, I’m going to assume that your compiler supports C++11 lambda expressions and the auto keyword. If not, it can be fairly trivially rewritten as a functor. But we’re looking for something roughly like this:

    std::transform(map_first, map_last, std::back_inserter(vec), [](std::pair<double,MyType> p) { return p.second; });
    

    Now we just need to fill in the two first parameters:

    auto map_first = std::next(map.end(), -n); 
    auto map_last = map.end();
    

    The only tricky part here is that map iterators are bidirectional, but not random-access, so we can’t simply say map.end() - n. The - operator is not defined. Instead, we have to use std::next (which takes linear rather than constant time for bidirectional operators, but there’s no way around that).

    (Note, I haven’t tried compiling this code, so it might require a small bit of tweaking)

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

Sidebar

Related Questions

Is there a idiomatic way of removing elements from PATH-like shell variables? That is
Given var dst, src map[K]V I can copy all entries from src into dst
What is the Ruby idiomatic way for retrieving a single character from a string
What is the idiomatic Python way to test if all elements in a collection
What's the idiomatic way to do maximumBy (higher order function taking a comparison function
Is there an idiomatic way to get available namespaces that can be used? (all-ns)
What is the (or is there an) idiomatic way to strip newlines from strings
What is the idiomatic way to delete old documents from a Lucene Index? I
In F# what is the most functional and idiomatic way of creating or newing
What is the most idiomatic way to do the following? def xstr(s): if s

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.