我认为将 std::map 键提取到 std::vector 应该可以在不为 gcc (4.6) 指定 --std=c++0x 标志的情况下工作,但事实并非如此。知道为什么吗?
template <typename Map, typename Container>
void extract_map_keys(const Map& m, Container& c) {
struct get_key {
typename Map::key_type operator()
(const typename Map::value_type& p) const {
return p.first;
}
};
transform(m.begin(), m.end(), back_inserter(c), get_key());
}
谢谢!
最佳答案
原因是您使用本地类型 get_key
作为最后一个参数。这在 C++98 中是不允许的,并且 C++11 的规则已更改/放宽。
这可以在 this example 中看到:
template <class T> bool cpp0X(T) {return true;} //cannot be called with local types in C++03
bool cpp0X(...){return false;}
bool isCpp0x()
{
struct local {} var;
return cpp0X(var);
}
关于c++ - 为什么这个 std::map 键提取函数需要 --std=c++?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24848319/