开发者

Getting a list of values from a map

开发者 https://www.devze.com 2023-01-25 22:04 出处:网络
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::开发者_JAVA技巧list&

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::开发者_JAVA技巧list<B> (or set for that matter. Is there a built-in stl way to do this?


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;} );


There's nothing built in, no. It's simple enough to write your own function, though: Iterate over the map. The iterator will give you a pair<A, B>. Add each second value to the result list.


You can't just "get" such a list because there is no pre-existing list stored anywhere in the guts, but you can build one:

typedef std::map<A,B> myMapType;
myMapType myMap;
std::list<B> valueList;
for (myMapType::const_iterator it=myMap.begin(); it!=myMap.end(); ++it) {
  valueList.push_back( it->second );
}

Or if you really like the more STL way:

class GetSecond {
  template<typename T1, typename T2>
  const T2& operator()( const std::pair<T1,T2>& key_val ) const
    { return key_val.second; }
};

typedef std::map<A,B> myMapType;
myMapType myMap;
std::list<B> valueList;
std::transform(myMap.begin(), myMap.end(), std::back_inserter(valueList),
               GetSecond());


One of many "built-in" ways is of course the most obvious one. Just iterate over all pair elements, which are ordered by key (pair::first), and add the value (pair::second) to a new container, which you can construct with the correct capacity to get rid of excess allocations during the iteration and adding.

Just a note: std::list is seldom the container you actually want to be using. Unless, of course, you really, really do need its specific features.


Sure.

std::list<B> list;
std::for_each(myMap.begin(), myMap.end(), [&](const std::pair<const A, B>& ref) {
    list.push_back(ref.second);
});

If you don't have a C++0x compiler, first you have my sympathies, and second, you will need to build a quick function object for this purpose.


You can use boost's transform_iterator: http://www.boost.org/doc/libs/1_64_0/libs/iterator/doc/transform_iterator.html

struct GetSecond {
  template <typename K, typename T>
  const T& operator()(const std::pair<K, T> & p) const { return p.second; }
  template <typename K, typename T>
  T& operator()(std::pair<K, T> & p) const { return p.second; }
};

template <typename MapType>
  auto begin_values(MapType& m) -> decltype(boost::make_transform_iterator(m.begin(), GetSecond())) {
  return boost::make_transform_iterator(m.begin(), GetSecond());
}

template <typename MapType>
  auto end_values(MapType& m) -> decltype(boost::make_transform_iterator(m.end(), GetSecond())) {
  return boost::make_transform_iterator(m.end(), GetSecond());
}

template <typename MapType>
  struct MapValues {
  MapType & m;
  MapValues(MapType & m) : m(m) {}
  typedef decltype(begin_values(m)) iterator;
  iterator begin() { return begin_values(m); }
  iterator end() { return end_values(m); }
};

template <typename MapType>
  MapValues<MapType> get_values(MapType & m) {
  return MapValues<MapType>(m);
}


int main() {
  std::map<int, double> m;
  m[0] = 1.0;
  m[10] = 2.0;
  for (auto& x : get_values(m)) {
    std::cout << x << ',';
    x += 1;
  }
  std::cout << std::endl;
  const std::map<int, double> mm = m;
  for (auto& x : get_values(mm)) {
    std::cout << x << ',';
  }
  std::cout << std::endl;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号