【发布时间】:2022-01-22 12:35:19
【问题描述】:
什么是遍历 boost::bimap 的所有键并将其存储到向量中的简单方法。 它会像我们对 std::map 那样工作吗
【问题讨论】:
标签: c++ dictionary boost
什么是遍历 boost::bimap 的所有键并将其存储到向量中的简单方法。 它会像我们对 std::map 那样工作吗
【问题讨论】:
标签: c++ dictionary boost
这是一种方式:
#include <boost/bimap.hpp>
#include <vector>
template<class L, class R>
std::vector<L> to_vector(const boost::bimap<L, R>& bm) {
std::vector<L> rv;
rv.reserve(bm.size()); // or bm.size() * 2 if you want to store the right keys too
for(auto&[l, r] : bm) { // loop through all the entries in the bimap
rv.push_back(l); // store the left key
// rv.push_back(r); // if you want to store the right key too
}
return rv;
}
然后调用它:
auto vec = to_vector(your_bimap);
【讨论】: