c++ - Assigning to an element via iterator -
i thought can assign new value elemnt of container through non-const iterator. tried that:
#include<iostream> #include<set> typedef std::set<int>::iterator it; std::set<int> set_int; int main() { set_int.insert(1); = set_int.begin(); *i = 11; //error: assignment of read-only location } and failed. why did fail?
according standard n4431 §23.2.4/6 associative containers [associative.reqmts] (emphasis mine):
iterator of associative container of bidirectional iterator category. for associative containers value type same key type, both iterator , const_iterator constant iterators. unspecified whether or not iterator , const_iterator same type. [ note: iterator , const_-iterator have identical semantics in case, , iterator convertible const_iterator. users can avoid violating 1 definition rule using const_iterator in function parameter lists.— end note ].
std::set associative container value type same key type, thus, std::set<t>::iterator constant iterator. consequently, failure you're getting justified.
Comments
Post a Comment