【问题标题】:c++ - how to list all keys inside a boost::bimapc++ - 如何列出 boost::bimap 中的所有键
【发布时间】:2022-01-22 12:35:19
【问题描述】:

什么是遍历 boost::bimap 的所有键并将其存储到向量中的简单方法。 它会像我们对 std::map 那样工作吗

【问题讨论】:

    标签: c++ dictionary boost


    【解决方案1】:

    这是一种方式:

    #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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多