1

我在unique_ptr 中间的const_iterator 上使用std::rotate 时遇到问题。

我试过了:

std::vector<std::unique_ptr<Object> >::const_iterator middle;
// middle is pointing at somewhere within vec defined elsewhere.
std::rotate(vec.begin(), middle, vec.end());

这导致调用 rotate(...iterator, ...const_iterator &, ...iterator) 没有匹配的函数

因此我尝试了这个: std::vector >::const_iterator first = vec.begin(); std::vector >::const_iterator last = vec.end(); 标准::旋转(第一,中间,最后);

然后它会给出一堆错误消息,如下所示。这是一个错误还是我犯了一些错误?

In file included from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
             from file.cpp:8:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In function ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1601:22: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1603:5: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(std::unique_ptr<_Tp, _Tp_Deleter>&&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1624:22: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1626:5: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(std::unique_ptr<_Tp, _Tp_Deleter>&&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_tree.h:62:0,
             from /usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/map:60,
             from dir/file3.hpp:4,
             from dir/file2.hpp:3,
             from file.hpp:3,
             from file.cpp:1:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h: In static member function ‘static void std::__iter_swap<_BoolType>::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _ForwardIterator2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, bool _BoolType = false]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:138:7:   instantiated from ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _ForwardIterator2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1609:5:   instantiated from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:207:7: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>::unique_ptr(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:88:31: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:89:11: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:208:19: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:89:11: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:90:11: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(std::unique_ptr<_Tp, _Tp_Deleter>&&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h: In static member function ‘static _OI std::__copy_move<true, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const std::unique_ptr<Object>*, _OI = const std::unique_ptr<Object>*]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:404:70:   instantiated from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = true, _II = const std::unique_ptr<Object>*, _OI = const std::unique_ptr<Object>*]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:442:39:   instantiated from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = true, _II = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _OI = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:506:47:   instantiated from ‘_OI std::move(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _OI = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1602:5:   instantiated from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:368:8: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:208:19: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:368:8: error: used here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h: In static member function ‘static _BI2 std::__copy_move_backward<true, false, std::random_access_iterator_tag>::__copy_move_b(_BI1, _BI1, _BI2) [with _BI1 = const std::unique_ptr<Object>*, _BI2 = const std::unique_ptr<Object>*]’:
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:601:18:   instantiated from ‘_BI2 std::__copy_move_backward_a(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = const std::unique_ptr<Object>*, _BI2 = const std::unique_ptr<Object>*]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:610:34:   instantiated from ‘_BI2 std::__copy_move_backward_a2(_BI1, _BI1, _BI2) [with bool _IsMove = true, _BI1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _BI2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:681:15:   instantiated from ‘_BI2 std::move_backward(_BI1, _BI1, _BI2) [with _BI1 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >, _BI2 = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1625:5:   instantiated from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:1677:7:   instantiated from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<const std::unique_ptr<Object>*, std::vector<std::unique_ptr<Object> > >]’
file.cpp:76:35:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:566:6: error: passing ‘const std::unique_ptr<Object>’ as ‘this’ argument of ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’ discards qualifiers
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/unique_ptr.h:208:19: error: deleted function ‘std::unique_ptr<_Tp, _Tp_Deleter>& std::unique_ptr<_Tp, _Tp_Deleter>::operator=(const std::unique_ptr<_Tp, _Tp_Deleter>&) [with _Tp = Object, _Tp_Deleter = std::default_delete<Object>, std::unique_ptr<_Tp, _Tp_Deleter> = std::unique_ptr<Object>]’
/usr/lib/gcc/x86_64-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algobase.h:566:6: error: used here
make: *** [file.o] Error 1
4

2 回答 2

3

您不能rotateconst_iterators 一起使用,因为rotate会更改指定范围的元素。相反,您必须使用iterators。

于 2011-07-19T23:33:00.200 回答
2

如果要信任cplusplus 文档,则本质上是调用,因此您不能使用s.rotateswap(T &, T &)const_iterator

(我想有人可能会要求rotate在 const_iterators 上运行的 for 列表的专用版本。不过,我认为您可以相当容易地构建一个。)

于 2011-07-19T23:33:03.000 回答