代码之家  ›  专栏  ›  技术社区  ›  Tronic

将对象从一个Boost ptr_容器移动到另一个容器

  •  4
  • Tronic  · 技术社区  · 16 年前

    我想将某些元素从a移到b:

    boost::ptr_vector<Foo> a, b;
    // ...
    b.push_back(a.release(a.begin() + i)));
    

    boost::ptr_container_detail::static_move_ptr<...> ,不适合向后推。

    我应该如何进行?

    .get() .release() 这提供了一个原始指针(这也可能导致一些异常安全问题)。然而,我不希望依赖于未记录的内部功能,所以请随意分享任何更好的解决方案。。。

    2 回复  |  直到 16 年前
        1
  •  4
  •   Kirill V. Lyadvinsky    16 年前
    boost::ptr_vector<Foo> a, b;
    
    // transfer one element a[i] to the end of b
    b.transfer( b.end(), a.begin() + i, a ); 
    // transfer N elements a[i]..a[i+N] to the end of b
    b.transfer( b.end(), a.begin() + i, a.begin() + i + N, a );
    
        2
  •  0
  •   Warpin    16 年前

    就个人而言,我更喜欢使用std::vector<&燃气轮机;boost::shared_ptr(即std::vector>a、 b)。

    然后您可以使用标准矢量函数。