插入向量时提供迭代器?? [英] Supplying an iterator when inserting into vector??

查看:60
本文介绍了插入向量时提供迭代器??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么插入仅在指定迭代器时加上要插入

的对象:


std :: vector< intt;

std :: vector< int> :: iterator it;

it = t.begin();


t.insert(它, 33);


如果我使用push_back而不是我不需要提供迭代器。但是什么

是插件只适用于迭代器的原因?

解决方案

desktop写道:
< blockquote class =post_quotes>
为什么插入只在指定迭代器加上对象时才起作用

要插入:


std :: vector< intt;

std :: vector< int> :: iterator it;

it = t.begin();


t.insert(it,33);


如果我使用push_back而不是我不需要提供迭代器。但是

插入只适用于迭代器的原因是什么?



呃...''push_back(value)''确实''插入(end(),value)''。如果你不给迭代器,你怎么把
插入到除了结尾之外的任何地方?


如果你想要一个超载''插入' '没有一个交互者,什么

它与''push_back'之间的区别是什么?你是否期望

只做插入开头?


你不明白''insert''是一个通用函数来放置

向量中的值_anywhere_?


V

-

请删除资金' 'A'在通过电子邮件回复时

我没有回复最热门的回复,请不要问


在6月4日上午8:29,桌面< f ... @ssss.comwrote:


为什么插入仅在指定迭代器加上对象时才起作用

插入:


std :: vector< intt;

std :: vector< int> :: iterator it;

it = t.begin();


t.insert(it,33);


如果我使用push_back代替我不需要提供迭代器。但是什么

是插入只适用于迭代器的原因?



push_back默认情况下插入容器的末尾,而插入

允许你提到你可以插入的位置。


Naresh Rautela写道:


6月4日上午8:29,桌面< f。 .. @ sss.comwrote:


>为什么插入仅在指定迭代器加上要插入的对象时起作用:

std :: vector< intt;
std :: vector< int> :: iterator it;
it = t.begin();

t.insert(它,33);

如果我使用push_back而不是我不需要提供迭代器。但是什么
是插件只与迭代器一起工作的原因?



push_back默认插入容器的末尾,而插入

允许你提到你可以插入的位置。



好​​的谢谢你的信息。当我想从

向量中提取元素时,我会使用类似的东西:


t.at(0);

但是如果我有一个包含我自己的对象的向量,我已经在

类''test''中定义了(使用getInt()函数返回一个私有整数)我

想为此使用迭代器:


std :: vector< test * t2;

std :: vector< test * > :: iterator it2;

it2 = t2.begin();

test * tt2 = new test(707);

t2 .insert(it2,tt2);

* it2-> getInt()


但这不起作用。迭代器只用于插入而不是用于提取自定义对象吗?


Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don''t need to supply the iterator. But what
are the reason the insert only works with an iterator?

解决方案

desktop wrote:

Why does insert only work when specifying an iterator plus the object
to be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don''t need to supply the iterator. But
what are the reason the insert only works with an iterator?

Uh... ''push_back(value)'' does ''insert(end(), value)''. How would you
insert into any place except the end if you don''t give an iterator?

And if you wanted an overloaded ''insert'' without an interator, what
would the difference be between it and ''push_back''? Do you expect it
to only do "insert into beginning"?

Do you not understand that ''insert'' is a generic function to place
the value _anywhere_ in the vector?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


On Jun 4, 8:29 am, desktop <f...@sss.comwrote:

Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don''t need to supply the iterator. But what
are the reason the insert only works with an iterator?

push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.


Naresh Rautela wrote:

On Jun 4, 8:29 am, desktop <f...@sss.comwrote:

>Why does insert only work when specifying an iterator plus the object to
be inserted:

std::vector<intt;
std::vector<int>::iterator it;
it = t.begin();

t.insert(it,33);

If I use push_back instead I don''t need to supply the iterator. But what
are the reason the insert only works with an iterator?


push_back by default inserts at the end of the container while insert
allows you to mention a position where you can insert.

Ok thanks for the info. When I want to extract an element from the
vector I would use something like:

t.at(0);

But if I have a vector containing my own object that I have defined in a
class ''test'' (with a getInt() function returning a private integer) I
would like to use iterators for this:

std::vector<test*t2;
std::vector<test*>::iterator it2;
it2 = t2.begin();
test* tt2 = new test(707);
t2.insert(it2,tt2);
*it2->getInt()

But this does not work. Are iterators only used for inserting and not
extracting custom objects?


这篇关于插入向量时提供迭代器??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆