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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:44:42+00:00 2026-05-18T01:44:42+00:00

Is there an stl way to get a list of values from a map?

  • 0

Is there an stl way to get a list of values from a map?

i.e, I have:

std::map<A,B> myMap;

and I would like a function that will return just the list of values, i.e, std::list<B> (or set for that matter.
Is there a built-in stl way to do this?

  • 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-18T01:44:43+00:00Added an answer on May 18, 2026 at 1:44 am

    A map element is defined as a map::value_type, and the type of it is a pair<A,B>. first is the key and second is the value. You can write a functor to extract second from a value_type, and copy that in to a vector (or a list, or whatever you want.) The best way to do the copying is to use transform, which does just what its name implies: it takes a value of one type and transforms it to a different type of value.

    Here’s a complete working example:

    #include <cstdlib>
    #include <map>
    #include <string>
    #include <algorithm>
    #include <iterator>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    typedef map<unsigned, string> MyMap;
    MyMap my_map;
    
    struct get_second : public std::unary_function<MyMap::value_type, string>
    {
        string operator()(const MyMap::value_type& value) const
        {
            return value.second;
        }
    };
    
    int main()
    {
        my_map[1] = "one";
        my_map[2] = "two";
        my_map[3] = "three";
        my_map[4] = "four";
        my_map[5] = "five";
    
        // get a vector of values
        vector<string> my_vals;
        transform(my_map.begin(), my_map.end(), back_inserter(my_vals), get_second() );
    
        // dump the list
        copy( my_vals.begin(), my_vals.end(), ostream_iterator<string>(cout, "\n"));
    }
    

    EDIT:

    If you have a compiler that supports C++0x lambdas, you can eliminate the functor entirely. This is very useful for making code more readable and, arguable, easier to maintain since you don’t end up with dozens of little one-off functors floating around in your codebase. Here’s how you would change the code above to use a lambda:

    transform(my_map.begin(), my_map.end(), back_inserter(my_vals), [](const MyMap::value_type& val){return val.second;} );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an STL map ; i would like to get the first non
Is there any way to return an STL iterator to a std::map (e.g. std::map<const
Is there any way to get all keys from an STL hash_map? Or I
Since there is no .resize() member function in C++ std::map I was wondering, how
I would know if there is a way to pass arguments using std::mem_fun ?
I have a multimap and I would like to get a set of sets
Is there a way to automatically lock an STL container on access, without having
I particularly like the simplicity of using STL containers in the straightforward way. I
here is a quite simple question(I think), is there a STL library method that
Is there an easy way to get some str/unicode object represented as a big

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.