将唯一指针的向量复制到新向量中

时间:2014-07-21 14:30:49

标签: c++ c++11 vector const

当我编译下面的代码时,我收到编译错误:

std::vector<std::unique_ptr<boxIndex>> tmpVec;
for(const auto& it: hrzBoxTmpMap){
    for(const auto& it2: hrzBoxVec){
        std::copy_if(hrzBoxVec.begin(), hrzBoxVec.end(), tmpVec.begin(), [&](std::unique_ptr<boxIndex>& p)
        {
            return !(it.second == p->getTop() &&
                     it.first != p->getLeft() );
        });
    }
}

编译错误是:

/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_algo.h: 

> In instantiation of ‘_OIter std::copy_if(_IIter, _IIter, _OIter,
> _Predicate) [with _IIter = __gnu_cxx::__normal_iterator<std::unique_ptr<boxIndex>*, std::vector<std::unique_ptr<boxIndex> > >; _OIter =
> __gnu_cxx::__normal_iterator<std::unique_ptr<boxIndex>*, std::vector<std::unique_ptr<boxIndex> > >; _Predicate =
> smoothHrzIndexing(std::vector<std::unique_ptr<boxIndex>
> >&)::<lambda(std::unique_ptr<boxIndex>&)>]’: test_word_2.cpp:282:5:   required from here
> /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/stl_algo.h:990:6:
> error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>&
> std::unique_ptr<_Tp, _Dp>::operator=(const std::unique_ptr<_Tp, _Dp>&)
> [with _Tp = boxIndex; _Dp = std::default_delete<boxIndex>]’ In file
> included from
> /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/memory:86:0,
>                  from test_word_2.cpp:8: /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../include/c++/4.7.2/bits/unique_ptr.h:263:19:
> error: declared here

有人可以帮我吗?

3 个答案:

答案 0 :(得分:7)

你可以做的是使用std::move_iterator<...>使用,例如下面的内容(这是SSCCE展示关键点:

#include <iostream>
#include <iterator>
#include <vector>
#include <memory>

int main()
{
    std::vector<std::unique_ptr<int>> boxVec;
    boxVec.emplace_back(new int(1));
    boxVec.emplace_back(new int(17));
    boxVec.emplace_back(new int(3));
    std::vector<std::unique_ptr<int>> tmpVec;
    std::copy_if(std::make_move_iterator(boxVec.begin()),
                 std::make_move_iterator(boxVec.end()),
                 std::back_inserter(tmpVec),
                 [&](std::unique_ptr<int> const& p){
                     return *p == 17; });
    for (auto const& x: boxVec) {
        (x? std::cout << *x: std::cout << "<null>") << " ";
    }
    std::cout << "\n";
}

取消引用std::move_iterator<It>将返回迭代器值类型的合适rvalue。由于rvalue是使用std::move(*it)获得的,因此它是一个参考。也就是说,在实际移动该值之前,该值不会被窃取。比较使用const&,即它不会窃取该值。赋值将成为右值赋值。该代码还使用std::back_inserter()来安排足够的元素在目标中。

我不认为这确实是一个可靠的解决方案,但我也不认为有像std::move_if()这样的算法(或任何算法的自定义导致相同的行为)。为了真正处理有条件移动的对象,我认为你为传递给谓词的值和对象的分配方式提供了不同的访问机制(property maps将解决这些问题,但是没有提议,将它们添加到C ++标准中。)

答案 1 :(得分:3)

没有直接的方法可以做到这一点,但你可以链接一些STL命令来实现你想要的:

  • std::stable_partitionstd::partition将您的容器分成两半。
  • std::move将要移动的值移动到新的向量
  • vector.erase删除旧的,无效的unique_ptr

最后,你有一个干净的源向量(所有移动,删除无效的条目)和一个干净的目标向量。

这可能看起来像这样:

std::vector<std::unique_ptr<int>> source, target;
// ... fill data
auto it = std::stable_partition(begin(source),end(source),[](std::unique_ptr<int> const& val) {
    return *val < 3; // your negated condition
});
std::move(it,end(source),std::back_inserter(target));
source.erase(it,end(source));

Here is a live example

答案 2 :(得分:0)

std::unique_ptr 可移动,而非可移动
因此,您无法将std::copy_if()unique_ptr个实例一起使用。

如果您真的想使用std::copy_if(),那么您可以使用std::shared_ptr代替std::unique_ptr,然后如果您想要删除旧的矢量内容,只需使用它来销毁它例如vector::clear()

或者,如果shared_ptr对您来说太多开销,或者在任何情况下您只想要一个unique_ptr s的向量,那么您可能需要考虑进行就地删除不需要的向量元素使用std::remove_if()erase-remove idiom

相关问题