使用STL从map <T,Z>的键中填充vector <T>

Mr.*_*Boy 10 c++ stl

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

是否有更好的1行版本使用一些STL功能?

编辑:使用c ++ 11!

Adr*_*thy 8

便携性:

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

我认为STL的一些实现有一个非标准的扩展名select1st,这相当于SelectKey这里所示.正如K-Ballo在评论中指出的那样,还有一个TR1版本.我喜欢明确命名的版本,因为它更容易看到发生了什么.

由于不需要状态,你可以通过使用实际函数而不是函数来减少样板量:

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

如果你可以使用C++ 11,你可以使用lambda,它将选择代码保持在它所使用的位置附近:

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

甚至是基于范围的for循环,这可能是最优雅和可读的:

for(const auto &x : m) {
  v.push_back(x.first);
}
Run Code Online (Sandbox Code Playgroud)

  • 不应该对结构进行模板化,而应该是`operator()`.如果你真的想要对结构进行模板化,你应该在创建它时提供参数:`SelectKey <const T,V>()`. (3认同)
  • 你的`operator()`应该是`const`.在TR1中,可以使用`std :: bind(&map <T,Z> :: value_type :: first,_1)`来实现. (2认同)