b***y 发帖数: 2799 | 1 copy(a.begin(), a.end(), inserter(b, b.begin());
copy(a.begin(), a.end(), front_inserter(b)); //inverse order of a.
这两行的结果是不同的。是不是每插入一个a的element,都要调用一次后面的inserter
,因此也就每次都调用b.begin()?这样用inserter插入的,也应该是倒序啊。 | t****t 发帖数: 6806 | 2 inserter() returns a insert_iterator, which calls container.insert() each
time insert_iterator::operator= is called. The begin() is stored in the
insert_iterator. Thus it has limitations. For some sequential containers,
such as vector, insert() may invalidate the calculated (only once) begin(),
so it could be undefined.
back_inserter() returns a back_insert_iterator, which calls container.push_
back() each time back_insert_iterator::operator= is called. Thus it only
works for sequential containe
【在 b***y 的大作中提到】 : copy(a.begin(), a.end(), inserter(b, b.begin()); : copy(a.begin(), a.end(), front_inserter(b)); //inverse order of a. : 这两行的结果是不同的。是不是每插入一个a的element,都要调用一次后面的inserter : ,因此也就每次都调用b.begin()?这样用inserter插入的,也应该是倒序啊。
| b***y 发帖数: 2799 | 3 you mean insert_iterator can't be used on vecters because inserting into a
vector always invalidated the iteraters after that position?
,
push_
【在 t****t 的大作中提到】 : inserter() returns a insert_iterator, which calls container.insert() each : time insert_iterator::operator= is called. The begin() is stored in the : insert_iterator. Thus it has limitations. For some sequential containers, : such as vector, insert() may invalidate the calculated (only once) begin(), : so it could be undefined. : back_inserter() returns a back_insert_iterator, which calls container.push_ : back() each time back_insert_iterator::operator= is called. Thus it only : works for sequential containe
| t****t 发帖数: 6806 | 4 for vector:
Reallocation invalidates all the references, pointers, and iterators
referring to the elements in the sequence. It is guaranteed that no
reallocation takes place during insertions that happen after a call
to reserve() until the time when an insertion would make the size of
the vector greater than the size specified in the most recent call
to reserve().
for deque:
An insert in the middle of the deque invalidates all the iterators
and references to
【在 b***y 的大作中提到】 : you mean insert_iterator can't be used on vecters because inserting into a : vector always invalidated the iteraters after that position? : : , : push_
| b***y 发帖数: 2799 | 5 thanks for your explanation. I think inserter can be used on list even it
's a sequential container. What's the point to use inserter on associative
containers?
【在 t****t 的大作中提到】 : for vector: : Reallocation invalidates all the references, pointers, and iterators : referring to the elements in the sequence. It is guaranteed that no : reallocation takes place during insertions that happen after a call : to reserve() until the time when an insertion would make the size of : the vector greater than the size specified in the most recent call : to reserve(). : for deque: : An insert in the middle of the deque invalidates all the iterators : and references to
| t****t 发帖数: 6806 | 6 yes, you can use inserter on list<>, since insertion on list doesn't
invalidate iterator. however, since list<> can be splice()'ed with constant
time, i think inserter on list<> is rarely needed.
for associative container, e.g.
set x;
copy(istream_iterator(cin), istream_iterator(), inserter(x, x.
begin());
it
【在 b***y 的大作中提到】 : thanks for your explanation. I think inserter can be used on list even it : 's a sequential container. What's the point to use inserter on associative : containers?
| b***y 发帖数: 2799 | 7 多谢了。
constant
【在 t****t 的大作中提到】 : yes, you can use inserter on list<>, since insertion on list doesn't : invalidate iterator. however, since list<> can be splice()'ed with constant : time, i think inserter on list<> is rarely needed. : for associative container, e.g. : set x; : copy(istream_iterator(cin), istream_iterator(), inserter(x, x. : begin()); : : it
|
|