c++ - why the iterators can't refer to the container they belong to when container calls assign? -
string s="zhangzhizhong"; s.assign(s.begin()+2, s.end()-2); string correct! vector<int> ivec{0,1,2,3,4,5}; ivec.assign(ivec.begin()+1, ivec.end()-1); vector correct!!!
the above code correct, written in book iterators can't refer container belong when container calls assign()
.
this illegal sequential containers, vector
or deque
.
[sequence.reqmts]/4 table 100
a.assign(i,j)
pre:i
,j
not iteratorsa
but believe it's explicitly made valid std::string
:
[string::assign]/20
template<class inputiterator> basic_string& assign(inputiterator first, inputiterator last);
effects: equivalent
assign(basic_string(first, last))
.
the standard requires implementation make copy of sequence before performing assignment (or @ least, behave if does). there no requirement can see iterators not point string being assigned.
i'm not sure why doesn't work way containers, if had guess, i'd it's precisely avoid forcing implementation make copies.
Comments
Post a Comment