相关疑难解决方法(0)

如何从std :: map中检索所有键(或值)并将它们放入向量中?

这是我出来的可能方式之一:

struct RetrieveKey
{
    template <typename T>
    typename T::first_type operator()(T keyValuePair) const
    {
        return keyValuePair.first;
    }
};

map<int, int> m;
vector<int> keys;

// Retrieve all keys
transform(m.begin(), m.end(), back_inserter(keys), RetrieveKey());

// Dump all keys
copy(keys.begin(), keys.end(), ostream_iterator<int>(cout, "\n"));
Run Code Online (Sandbox Code Playgroud)

当然,我们也可以通过定义另一个仿函数RetrieveValues从地图中检索所有值.

有没有其他方法可以轻松实现这一目标?(我总是想知道为什么std :: map不包含我们这样做的成员函数.)

c++ dictionary stl stdmap

222
推荐指数
16
解决办法
30万
查看次数

标签 统计

c++ ×1

dictionary ×1

stdmap ×1

stl ×1