c++ - 将唯一指针 vector 复制到新 vector 中

标签 c++ c++11 vector constants

当我编译下面的代码时,出现编译错误:

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

有人可以帮我解决这个问题吗?

最佳答案

你可以做的是使用 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>将返回迭代器值类型的合适右值。由于右值是使用 std::move(*it) 获得的,这是一个引用。也就是说,在实际移动值之前,该值不会被盗。比较使用 const& ,即它不会窃取值(value)。该赋值将成为右值赋值。该代码还使用 std::back_inserter()安排足够的元素到达目的地。

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

关于c++ - 将唯一指针 vector 复制到新 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867679/

相关文章:

C++ 段错误 -tinyXML

c++ - QSyntaxHighlighter 不创建 QTextFragments

c++ - 检查序列容器在内存中是否连续

c++ - 当初始化列表中没有更多元素时,是否初始化数组的其余元素

c++ - 试图向现有 std::vector 添加元素的堆损坏

c++ - 写入文件

c++ - 高效的感知重要点 (PIP) 算法。在 R 或 Rcpp 中

c++ - 单元测试应用程序到硬件的接口(interface)——模拟与否

c++ - 定义 vector 元素的破坏顺序是否合理?

vector - 在对象 vector 上使用 find_if