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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:14:48+00:00 2026-05-26T00:14:48+00:00

map<T,Z> m= …; vector<T> v; v.reserve(m.size); for(map<T,Z>::iterator it=m.begin();it!=m.end();++it) { v.push_back(it->first); } Is there a

  • 0
map<T,Z> m= ...;
vector<T> v;
v.reserve(m.size);
for(map<T,Z>::iterator it=m.begin();it!=m.end();++it)
{
 v.push_back(it->first);
}

Is there a nicer 1-line version using some STL function(s)?

edit: not using c++11!

  • 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-26T00:14:49+00:00Added an answer on May 26, 2026 at 12:14 am

    Portable:

    struct SelectKey {
      template <typename F, typename S>
      F operator()(const std::pair<const F, S> &x) const { return x.first; }
    };
    
    std::transform(m.cbegin(), m.cend(), std::back_inserter(v), SelectKey());
    

    I think some implementations of STL have a non-standard extension called select1st, which is the equivalent of SelectKey shown here. As K-Ballo pointed out in the comments, there’s also a TR1 version. I like the explicitly-named version as it’s easier to see what’s going on.

    Since there’s no need for state, you can get away with slightly less boilerplate by using an actual function rather than a functor:

    template <typename F, typename S>
    F SelectKey()(const std::pair<const F, S> &x) { return x.first; }
    
    std::transform(m.cbegin(), m.cend(), std::back_inserter(v), SelectKey);
    

    If you could use C++11, you could use a lambda, which keeps the selection code close to where it’s used:

    std::transform(m.cbegin(), m.cend(), std::back_inserter(v),
                   [](const std::pair<const F, S> &x) { return x.first; });
    

    or even range-based for-loop, which is probably the most elegant and readable:

    for(const auto &x : m) {
      v.push_back(x.first);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

std::map find/end both provides const_iterator and iterator, e.g. iterator end (); const_iterator end ()
map<string,string>::find seems to be returning garbage iterator, since i can access neither my_it->first nor
std::map<std::string, std::string> myMap; std::map<std::string, std::string>::iterator i = m_myMap.find(some_key_string); if(i == m_imagesMap.end()) return NULL; string
Map<K,V<List<K>> graph = new HashMap<K,V<List<K>>(); Are there any major obstacles to using this to
std::map<any, string> is not working so I wonder if there's another approach to have
Does map() iterate through the list like for would? Is there a value in
std::map<Item*, item_quantity_t> _items; bool Inventory::hasItem(Item const& item) { return (_items.find(&item) != _items.end() ); };
Is there anyway to map Spring exceptions (eg. JpaObjectRetrievalFailureException ) to REST status code
I'm passing a map of strings from native c++ class to c# using c++/CLI.
map.resources :users do |user| user.resources :votes user.resources :voteable do |mv| mv.resources :votes end end

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.