是否有一个Java Map keySet()等效于C++的std :: map?

10 c++ java stl stdmap

是否存在与C++相同的Java Map keySet()std::map

Java keySet()方法返回"此映射中包含的键的设置视图".

Jam*_*lis 9

到目前为止提供的所有答案最终都会std::set直接创建,这可能并不理想:如果您只想迭代键,则不希望有创建全新容器的开销.

一个更灵活的选择是使用转换迭代器,它将迭代器转换std::map为某种类型的迭代器,在解除引用时只生成密钥.使用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>()
    );
}
Run Code Online (Sandbox Code Playgroud)

使用这些实用程序函数,您可以将任何范围的std::map迭代器(或迭代器转换为您可能拥有的任何其他对关联容器)转换为仅包含键的范围.举个例子:

#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, ","));
}
Run Code Online (Sandbox Code Playgroud)

该计划输出:

1,2,3,
Run Code Online (Sandbox Code Playgroud)

如果你确实想要std::set包含键,可以使用这些迭代器轻松创建一个:

std::set<int> s(begin_keys(m), end_keys(m));
Run Code Online (Sandbox Code Playgroud)

总的来说,这是一个更灵活的解决方案.

如果你没有Boost或者不想使用Boost或者不能使用Boost,那么这个特定的变换迭代器可以很容易地实现:

#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());   }
Run Code Online (Sandbox Code Playgroud)

此用法与Boost版本相同.


小智 1

也许以下内容可能有用:

#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;
}
Run Code Online (Sandbox Code Playgroud)

采用 STL 兼容序列(例如 std::vector、std::deque 或 std::list)的make_key_set函数的重载可以如下所示:

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 我唯一的问题是,这是一个经常需要的功能,但没有使其成为 STL 事实上的一部分。只鼓励编码错误。 (2认同)