Question

Is there a Java Map keySet() equivalent for C++'s std::map?

The Java keySet() method returns "a set view of the keys contained in this map."

Was it helpful?

Solution 3

Perhaps the following might be of use:

#include <iostream>
#include <iterator>
#include <algorithm>
#include <map>
#include <set>
#include <string>

template< class Key, 
          class T, 
          class Comparator,
          class MapAllocator,
          class SetAllocator>
void make_key_set(const std::map<Key,T,Comparator,MapAllocator>& map, 
                  std::set<Key,Comparator,SetAllocator>& set)
{
   set.clear();
   typedef typename std::map<Key,T,Comparator,MapAllocator> map_type;
   typename map_type::const_iterator itr = map.begin();
   while (map.end() != itr)
   {
      set.insert((itr++)->first);
   }
}

int main()
{
  std::map<std::string, double> m;

  m["one"] = 1.1;
  m["two"] = 2.2;
  m["three"] = 3.3;

  std::set<std::string> key_set;

  make_key_set(m,key_set); 

  std::copy(key_set.begin(), key_set.end(),
            std::ostream_iterator<std::string>(std::cout, "\n"));

  return  0;
}

An overload for the make_key_set function taking STL compatible sequences such as std::vector, std::deque or std::list can be as follows:

template< class Key, 
          class T, 
          class Comparator,
          class MapAllocator,
          class SeqAllocator,
          template<class,class> class Sequence>
void make_key_set(const std::map<Key,T,Comparator,MapAllocator>& map, 
                  Sequence<Key,SeqAllocator>& sequence)
{
   sequence.clear();
   typedef typename std::map<Key,T,Comparator,MapAllocator> map_type;
   typename map_type::const_iterator itr = map.begin();
   while (map.end() != itr)
   {
      sequence.push_back((itr++)->first);
   }
}

OTHER TIPS

All of the answers presented thus far end up creating a std::set directly, which may not be ideal: if you only want to be able to iterate over the keys, you don't want to have the overhead of creating a whole new container.

A more flexible option would be to use a transform iterator that converts a std::map iterator into some type of iterator that just yields the key when dereferenced. This is rather straightforward using the Boost Transform Iterator:

#include <functional>
#include <boost/iterator/transform_iterator.hpp>

// You may already have a select1st implementation; if not, you should :-)
template <typename Pair>
struct select1st
    : std::unary_function<const Pair&, const typename Pair::first_type&>
{
    const typename Pair::first_type& operator()(const Pair& p) const 
    { 
        return p.first; 
    }
};

template <typename C>
boost::transform_iterator<
    select1st<typename C::value_type>, typename C::const_iterator
> begin_keys(const C& c) 
{ 
    return boost::make_transform_iterator(
        c.begin(), select1st<typename C::value_type>()
    );
}

template <typename C>
boost::transform_iterator<
    select1st<typename C::value_type>, typename C::const_iterator
> end_keys(const C& c) 
{ 
    return boost::make_transform_iterator(
        c.end(), select1st<typename C::value_type>()
    );
}

With these utility functions, you can convert any range of std::map iterators (or iterators into any other pair associative container you may have) into a range of just the keys. As an example:

#include <iostream>
#include <iterator>
#include <map>

int main()
{
    std::map<int, int> m;
    m.insert(std::make_pair(1, 2));
    m.insert(std::make_pair(2, 4));
    m.insert(std::make_pair(3, 6));

    std::copy(
        begin_keys(m), end_keys(m), 
        std::ostream_iterator<int>(std::cout, ","));
}

This program outputs:

1,2,3,

If you really do want a std::set containing the keys, you can easily create one using these iterators:

std::set<int> s(begin_keys(m), end_keys(m));

Overall, it's a more flexible solution.

If you don't have Boost or don't want to use Boost or can't use Boost, this specific transform iterator can be implemented quite easily:

#include <iterator>

template <typename C>
class key_iterator
    : public std::iterator<
          std::bidirectional_iterator_tag, 
          typename C::key_type, 
          typename C::difference_type, 
          typename C::pointer, 
          typename C::reference
      >
{
public:

    key_iterator() { }
    explicit key_iterator(typename C::const_iterator it) : it_(it) { }

    typename const C::key_type& operator*() const  { return  it_->first; }
    typename const C::key_type* operator->() const { return &it_->first; }

    key_iterator& operator++() { ++it_; return *this; }
    key_iterator operator++(int) { key_iterator it(*this); ++*this; return it; }

    key_iterator& operator--() { --it_; return *this; }
    key_iterator operator--(int) { key_iterator it(*this); --*this; return it; }

    friend bool operator==(const key_iterator& lhs, const key_iterator& rhs)
    {
        return lhs.it_ == rhs.it_;
    }

    friend bool operator!=(const key_iterator& lhs, const key_iterator& rhs)
    {
        return !(lhs == rhs);
    }

private:

    typename C::const_iterator it_;
};

template <typename C>
key_iterator<C> begin_keys(const C& c) { return key_iterator<C>(c.begin()); }

template <typename C>
key_iterator<C> end_keys(const C& c)   { return key_iterator<C>(c.end());   }

The usage for this is the same as for the Boost version.

Here is a one-and-a-bit liner:

map<K,V> m;
...
// Useful stuff goes here
...
set<K> s;
transform(m.begin(), m.end(), inserter(s, s.begin()), select1st<pair<K,V> >());

If you don't have select1st:

template <class P>
struct select1st : public std::unary_function<P, typename P::first_type>
{
    const typename P::first_type& operator()(const P &arg) const { return arg.first; }
};

you can implement it yourself:

vector<T> keys;
for (map<T,S>::iterator it=m.begin(); it!=m.end; it++)
  keys.push_back(it->first)

Boost.Range provides this as boost::adaptors::map_values:

std::map my_map;
for (auto &value : my_map | boost::adaptors::map_values) {
    //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top