c++ - 如何列出 boost::bimap 中的所有键
c++ - how to list all keys inside a boost::bimap
迭代 boost::bimap 的所有键并将其存储到向量中的简单方法是什么。
它会像 std::map
那样工作吗
这是一种方式:
#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);
迭代 boost::bimap 的所有键并将其存储到向量中的简单方法是什么。 它会像 std::map
那样工作吗这是一种方式:
#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);