环境:C++11 ;Apple clang version 12.0.0 (clang-1200.0.32.29)
代码:
#include <algorithm>
#include <set>
#include <functional>
using namespace std;
int main(int argc, char const* argv[]) {
set<int> collSet{2,3,1,0,5};
transform(collSet.begin(), collSet.end(), collSet.begin(), negate<int>());
return 0;
}
错误提示:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1948:19: error: cannot assign to return value because function 'operator*' returns a const value
*__result = __op(*__first);
~~~~~~~~~ ^
transformset.cpp:9:5: note: in instantiation of function template specialization 'std::__1::transform<std::__1::__tree_const_iterator<int, std::__1::__tree_node<int, void *> *, long>, std::__1::__tree_const_iterator<int, std::__1::__tree_node<int, void
*> *, long>, std::__1::negate<int> >' requested here
transform(collSet.begin(), collSet.end(), collSet.begin(), negate<int>());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:921:31: note: function 'operator*' which returns const-qualified type 'std::__1::__tree_const_iterator<int, std::__1::__tree_node<int, void
*> *, long>::reference' (aka 'const int &') declared here
_LIBCPP_INLINE_VISIBILITY reference operator*() const
在书上和网上看到很多这样的写法,但是本地运行报错,求大佬指点迷津。