下面的代码:
#include <iostream>
#include <set>
#include <algorithm>
std::set<int> s;
int main()
{
s.insert(1);
s.insert(2);
std::remove(s.begin(), s.end(), 1);
}
不与GCC 4.7.2编译:
$ LANG=C g++ test.cpp
In file included from /usr/include/c++/4.7/algorithm:63:0,
from test.cpp:3:
/usr/include/c++/4.7/bits/stl_algo.h: In instantiation of '_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = std::_Rb_tree_const_iterator<int>; _Tp = int]':
test.cpp:12:38: required from here
/usr/include/c++/4.7/bits/stl_algo.h:1135:13: error: assignment of read-only location '__result.std::_Rb_tree_const_iterator<_Tp>::operator*<int>()'
所以我去的定义fset::iterator
我发现这个在gcc的实现(文件../c++/4.7/bits/stl_set.h
,从125):
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// DR 103. set::iterator is required to be modifiable,
// but this allows modification of keys.
typedef typename _Rep_type::const_iterator iterator;
typedef typename _Rep_type::const_iterator const_iterator;
typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
typedef typename _Rep_type::size_type size_type;
typedef typename _Rep_type::difference_type difference_type;
为什么两个定义常量? 为什么我的(很简单)的代码无法正常工作?