由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 关于inserter
相关主题
deque的pointer和reference是怎么回事?iterator and [], which is faster?
一个C++的概念问题stl 源代码疑问
stl container erase in a loop请教一个c++的问题
A C++ STL questionSTL iterator的疑问
stl的问题,为啥swap可能会invalidate end iterator?如何把文件内容读到2D的vector里?
有否可以O(1)remove 一个元素的java LinkedList推荐?一个小程序差点搞死了g++,怎么回事?
c++ pointers are iterators, why?请教c++的string vector问题,谢谢! (转载)
问一下STL里的queue, and stack 遍历的问题 (转载)C++中使用back_inserter为啥可以不用#include 和using std::back_inserter;??
相关话题的讨论汇总
话题: inserter话题: iterator话题: insert话题: vector话题: list
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
C++中使用back_inserter为啥可以不用#include 和using std::back_inserter;??stl的问题,为啥swap可能会invalidate end iterator?
说c++不难的欢迎来看看这个有否可以O(1)remove 一个元素的java LinkedList推荐?
mem_fun_ref为什么必须引用?c++ pointers are iterators, why?
intel icc hash_map 求救!问一下STL里的queue, and stack 遍历的问题 (转载)
deque的pointer和reference是怎么回事?iterator and [], which is faster?
一个C++的概念问题stl 源代码疑问
stl container erase in a loop请教一个c++的问题
A C++ STL questionSTL iterator的疑问
相关话题的讨论汇总
话题: inserter话题: iterator话题: insert话题: vector话题: list