如果我对stl :: list和const_iterator的(非const)引用到该列表中的特定位置,我如何获得指向同一位置的迭代器? (我想擦除该位置的元素,但list没有const_iterators的擦除方法)
这似乎不应该违反const-correctness,因为它只有在我对列表有非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