back_inserter和end()的帮助 [英] help for back_inserter and end()

查看:80
本文介绍了back_inserter和end()的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


迭代器适配器back_inserter拿一个容器并返回一个

迭代器,这样我们就可以将元素插入到容器的末尾。

出于好奇,我试着查看返回的元素

迭代器是指。这是我的代码:


#include< iostream>

#include< vector>

#include< iterator>

#include< algorithm>


使用命名空间std;


int main(){

vector< intv;

v.push_back(0);

v.push_back(1);

v.push_back(2) ;


cout<< (*(back_inserter(v)));

返回0;

}


上面的代码无法编译。由于back_inserter,返回一个

迭代器,然后我想我可以取消引用它。

有什么问题呢?


我还尝试使用复制复制容器,而不是

back_inserter,我使用了end()。代码是:


#include< iostream>

#include< vector>

#include< iterator>

#include< algorithm>


使用命名空间std;


int main(){

vector< intv;

v.push_back(0);

v.push_back(1);

v.push_back(2);


vector< intw;

v.push_back(3);

v.push_back(4);


copy(w.begin(),w.end(),v.end()); //而不是back_inserter,我

使用.end()


for(vector< int> :: const_iterator i = v.begin(); i != v.end(); i ++)

cout<< (* i)<<结束;


返回0;

}


我被告知上面的代码是错的,但令人惊讶的是,它编译并且

工作。我真的对back_inserter的内容感到困惑。是什么时候我可以用b $ b来代替back_inserter。


非常感谢!

解决方案

" Jess" < wd *** @ hotmail.comwrote in message

news:11 ********************* @ p15g2000hsd.googlegro ups。 com ...


迭代器适配器" back_inserter"拿一个容器并返回一个

迭代器,这样我们就可以将元素插入到容器的末尾。

出于好奇,我试着查看返回的元素

迭代器是指。这是我的代码:


#include< iostream>

#include< vector>

#include< iterator>

#include< algorithm>


使用命名空间std;


int main(){

vector< intv;

v.push_back(0);

v.push_back(1);

v.push_back(2) ;


cout<< (*(back_inserter(v)));

返回0;

}


上面的代码无法编译。由于back_inserter,返回一个

迭代器,然后我想我可以取消引用它。

有什么问题呢?



back_inserter是一个输出迭代器 - 你不能用它读,

只写。


我还尝试使用复制来复制容器,而不是使用bb和back_inserter,我使用了end()。代码是:


#include< iostream>

#include< vector>

#include< iterator>

#include< algorithm>


使用命名空间std;


int main(){

vector< intv;

v.push_back(0);

v.push_back(1);

v.push_back(2);


vector< intw;

v.push_back(3);

v.push_back(4);


copy(w.begin(),w.end(),v.end()); //而不是back_inserter,我

使用.end()


for(vector< int> :: const_iterator i = v.begin(); i != v.end(); i ++)

cout<< (* i)<<结束;


返回0;

}


我被告知上面的代码是错的,但令人惊讶的是,它编译并且

工作。



它工作只是在你所做的令人讨厌的事情发生的意义上,你不会咬人 - 你覆盖了矢量的结尾。取决于

向量的容量,或者在内存中跟随的任何内容,

副本可能会也可能不会造成明显的损害。但是矢量并没有因为你的显示循环应该显示出来而变得更大。


我真的很困惑什么是back_inserter我和

可以通过复制替换back_inserter。



好​​吧,back_inserter与

中的底层容器合作,根据需要增加它,而副本则没有。


HTH,

PJ Plauger

Dinkumware,Ltd。
http://www.dinkumware.com


感谢您的回复! />


它工作只是在你所做的令人讨厌的事情发生的意义上,你不会咬人 - 你覆盖了矢量的结尾。取决于

向量的容量,或者在内存中跟随的任何内容,

副本可能会也可能不会造成明显的损害。但是矢量并没有因为显示循环应该显示而变得更大。



我理解向量,容量不同于尺寸,

但是我不确定容量是多少影响结果。如果我使用end()

进行复制并假设我达到了容量限制,那么

将新元素添加到矢量,默默地被丢弃,因为矢量的容量没有增加?


" Jess" < wd *** @ hotmail.comwrote in message

news:11 ********************* @ n59g2000hsh.googlegro ups。 com ...


感谢您的回复!


>它工作只有在你所做的令人讨厌的事情发生的意义上才不会咬人 - 你覆盖了矢量的结尾。根据矢量的容量或内存中的任何内容,
副本可能会或可能不会造成明显的损坏。但是矢量并没有变得更大,因为你的显示循环应该显示出来。



我理解向量,容量不同于尺寸,

但是我不确定容量是多少影响结果。如果我使用end()

进行复制并假设我达到了容量限制,那么

将新元素添加到矢量,静静地

丢弃,因为矢量的容量没有增加?



让我们再试一次。复制功能*不会*增加向量的大小

。一个back_inserter将通过为向量调用insert;

但是只复制内存上的涂鸦。如果你有足够的容量,

你运气好,复制只是浪费时间(矢量不会让b $ b变得更大)。但是如果你不是,那么你写入了未知的土地,

可能搞乱另一个重要但完全不相关的对象。

在任何情况下,复制都不够聪明到默默地丢弃元素,

并且不会复制更改向量的大小或容量。


PJ Plauger

Dinkumware,Ltd 。
http://www.dinkumware.com


Hello,

The iterator adaptor "back_inserter" takes a container and returns a
iterator so that we can insert elements to the end of the container.
Out of curiosity, I tried to look at what element the returned
iterator refers to. Here is my code:

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;

int main(){
vector<intv;
v.push_back(0);
v.push_back(1);
v.push_back(2);

cout << (*(back_inserter(v)));
return 0;
}

The code above couldn''t compile. Since "back_inserter" returns an
iterator, then I would think I can dereference it. What''s wrong with
it?

I also tried to copy containers using "copy", and instead of
"back_inserter", I used "end()". The code is:

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;

int main(){
vector<intv;
v.push_back(0);
v.push_back(1);
v.push_back(2);

vector<intw;
v.push_back(3);
v.push_back(4);

copy(w.begin(),w.end(),v.end()); //instead of back_inserter, I
used .end()

for(vector<int>::const_iterator i = v.begin(); i != v.end(); i++)
cout << (*i) << endl;

return 0;
}

I was told the code above was wrong, but surprisingly, it compiled and
worked. I''m really puzzled by what "back_inserter" does and when I
can replace back_inserter by copy.

Thanks a lot!

解决方案

"Jess" <wd***@hotmail.comwrote in message
news:11*********************@p15g2000hsd.googlegro ups.com...

The iterator adaptor "back_inserter" takes a container and returns a
iterator so that we can insert elements to the end of the container.
Out of curiosity, I tried to look at what element the returned
iterator refers to. Here is my code:

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;

int main(){
vector<intv;
v.push_back(0);
v.push_back(1);
v.push_back(2);

cout << (*(back_inserter(v)));
return 0;
}

The code above couldn''t compile. Since "back_inserter" returns an
iterator, then I would think I can dereference it. What''s wrong with
it?

A back_inserter is an output iterator -- you can''t read with it,
only write.

I also tried to copy containers using "copy", and instead of
"back_inserter", I used "end()". The code is:

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;

int main(){
vector<intv;
v.push_back(0);
v.push_back(1);
v.push_back(2);

vector<intw;
v.push_back(3);
v.push_back(4);

copy(w.begin(),w.end(),v.end()); //instead of back_inserter, I
used .end()

for(vector<int>::const_iterator i = v.begin(); i != v.end(); i++)
cout << (*i) << endl;

return 0;
}

I was told the code above was wrong, but surprisingly, it compiled and
worked.

It "worked" only in the sense that the nasty thing you did happened
not to bite -- you overwrote the end of the vector. Depending on
the capacity of the vector, or whatever follows it in memory, the
copy may or may not cause apparent damage. But the vector doesn''t
get any bigger, as your display loop should have revealed.

I''m really puzzled by what "back_inserter" does and when I
can replace back_inserter by copy.

Well, back_inserter cooperates with the underlying container in
growing it as needed, while copy doesn''t.

HTH,

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


Thanks for the reply!

It "worked" only in the sense that the nasty thing you did happened
not to bite -- you overwrote the end of the vector. Depending on
the capacity of the vector, or whatever follows it in memory, the
copy may or may not cause apparent damage. But the vector doesn''t
get any bigger, as your display loop should have revealed.

I understand for vectors, the "capacity" is different to the "size",
but I''m not sure how "capacity" affects the result. If I used "end()"
for copying and suppose I reached the limit of the capacity, then will
the new elements, which are being added to the vector, be silently
discarded since the vector''s capacity is not increased?


"Jess" <wd***@hotmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...

Thanks for the reply!

>It "worked" only in the sense that the nasty thing you did happened
not to bite -- you overwrote the end of the vector. Depending on
the capacity of the vector, or whatever follows it in memory, the
copy may or may not cause apparent damage. But the vector doesn''t
get any bigger, as your display loop should have revealed.


I understand for vectors, the "capacity" is different to the "size",
but I''m not sure how "capacity" affects the result. If I used "end()"
for copying and suppose I reached the limit of the capacity, then will
the new elements, which are being added to the vector, be silently
discarded since the vector''s capacity is not increased?

Let''s try again. The copy function does *not* increase the size of
the vector. A back_inserter will, by calling insert for the vector;
but copy just scribbles on memory. If you have sufficient capacity,
you luck out and the copy is just a waste of time (the vector doesn''t
get any bigger). But if you don''t, then you write into terra incognita,
possibly messing up another important but completely unrelated object.
In no event will copy be smart enough to "silently discard" elements,
and in no event will copy change the size or capacity of the vector.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


这篇关于back_inserter和end()的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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