我在const_iterator上使用std :: rotate而不是在unique_ptr中间。
我试过了:
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());
这导致调用旋转没有匹配函数(... iterator,... const_iterator&amp;,... iterator)
因此我尝试了这个: std :: vector&gt; :: const_iterator first = vec.begin(); std :: vector&gt; :: const_iterator last = vec.end(); std :: rotate(first,middle,last);
然后它会给出一堆错误消息,如下所示。 这是一个错误还是我犯了一些错误?
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
答案 0 :(得分:3)
您不能将rotate
与const_iterator
一起使用,因为rotate
会更改指定范围的元素。相反,您必须使用iterator
s。
答案 1 :(得分:2)
如果cplusplus documentation为trusted,则rotate
基本上会调用swap(T &, T &)
,因此您无法使用const_iterator
。
(我想有人可能会为使用const_iterators的列表要求rotate
的专用版本。但我认为你可以很容易地构建一个。)