什么是迭代并將 boost::bimap 的所有鍵存盤到向量中的簡單方法。它會像我們對 std::map 一樣作業嗎
uj5u.com熱心網友回復:
這將是一種方式:
#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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/390244.html
