如果我有一个对 stl::list 的(非常量)引用和一个指向该列表中特定位置的 const_iterator,我怎样才能获得一个指向同一个地方的迭代器?(我想擦除该位置的元素,但列表没有 const_iterators 的擦除方法)
这似乎不应该违反 const 正确性,因为它仅在我对列表有非常量引用时才有效。
编辑:
等一下,看看 CPP 参考:http ://www.cplusplus.com/reference/list/list/erase/它说你可以用 const_iterator 调用 erase()。那么为什么我的编译器会抱怨呢?我正在使用 gcc 4.6 --std=c++0x
错误是:
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall --std=c++0x -c -fmessage-length=0 -MMD -MP -MF"src/constraint.d" -MT"src/constraint.d" -o "src/constraint.o" "../src/constraint.cpp"
../src/constraint.cpp: In member function ‘void constraint::remove_lit(std::list<std::pair<literal, int> >::const_iterator)’:
../src/constraint.cpp:144:18: error: no matching function for call to ‘std::list<std::pair<literal, int> >::erase(std::list<std::pair<literal, int> >::const_iterator&)’
../src/constraint.cpp:144:18: note: candidates are:
/usr/include/c++/4.6/bits/list.tcc:109:5: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator) [with _Tp = std::pair<literal, int>, _Alloc = std::allocator<std::pair<literal, int> >, std::list<_Tp, _Alloc>::iterator = std::_List_iterator<std::pair<literal, int> >]
/usr/include/c++/4.6/bits/list.tcc:109:5: note: no known conversion for argument 1 from ‘std::list<std::pair<literal, int> >::const_iterator {aka std::_List_const_iterator<std::pair<literal, int> >}’ to ‘std::_List_iterator<std::pair<literal, int> >’
/usr/include/c++/4.6/bits/stl_list.h:1160:7: note: std::list<_Tp, _Alloc>::iterator std::list<_Tp, _Alloc>::erase(std::list<_Tp, _Alloc>::iterator, std::list<_Tp, _Alloc>::iterator) [with _Tp = std::pair<literal, int>, _Alloc = std::allocator<std::pair<literal, int> >, std::list<_Tp, _Alloc>::iterator = std::_List_iterator<std::pair<literal, int> >]
/usr/include/c++/4.6/bits/stl_list.h:1160:7: note: candidate expects 2 arguments, 1 provided
make: *** [src/constraint.o] Error 1