关于std :: list的问题 [英] A Question about std::list

查看:84
本文介绍了关于std :: list的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您遍历列表中的对象列表时。


list< object> mylist;

list< object> :: const_iterator iter;

object ob;


for(iter = mylist.begin( ); iter!= mylist.end(); ++ iter)

{

ob = * iter;

ob.value = 10 ;

}


所以这里我正在遍历列表

但是声明是ob = * iter制作副本我列表中的一个元素?

或者我对mylist中的元素有所了解?

我在列表中复制一个元素似乎效率低下,当

我真正想要的只是一个指向对象的指针或对它的参考,以便

我可以修改它...


我认为iter->值不起作用...


评论?

解决方案

JustSomeGuy写道:

当您遍历列表中的对象列表时。

list< object> mylist;
list< object> :: const_iterator iter;
object ob;

for(iter = mylist.begin(); iter!= mylist.end(); + + iter)
{
ob = * iter;


你得到一份副本;

ob.value = 10;


您可以修改副本。现在,你可能想插入:


* iter = ob;

}

所以我在这里迭代列表
但是声明是ob = * iter在我的列表中制作一个元素的副本吗?
或者我对mylist中的元素有所了解?

这似乎效率低下我在列表中制作一个元素的副本,当我真正想要的是指向对象的指针或对它的引用时,我可以修改它...


是的,那效率很低。

我认为iter->值不起作用......




你实际上没试过,是吗?

最好


Kai-Uwe


< blockquote>

" JustSomeGuy" <无** @ nottelling.com>在消息中写道

新闻:DmtAc.757759


Pk3.612408@pd7tw1no ...

当您遍历列表列表中的对象。

list< object> mylist;
list< object> :: const_iterator iter;
object ob;

for(iter = mylist.begin(); iter!= mylist.end(); + + iter)
{
ob = * iter;
ob.value = 10;
}

所以我在这里迭代列表
但是声明ob = * iter在我的列表中制作一个元素的副本吗?
或者我对mylist中的元素有所了解吗?
对我来说复制一个元素似乎效率低下列表中的一个元素,当我真正想要的只是一个指向对象的指针或对它的参考,以便
我可以修改它...

我认为iter->值不起作用......

评论?




好​​奇,如果你想修改列表为什么呢?你使用const_iterator?如果

你想要一个指针或对列表中某个元素的引用,你为什么不用
只声明一个?


list< object> :: iterator iter;

object& ref;

object * ptr;


for(iter = mylist.begin(); iter!= mylist.end(); ++ iter)

{

ref = * iter;

ptr =& * iter;

ref.value = 10;

ptr-> value = 10;


但是,正如Kai-Uwe所说,只要''iter-> value = 10''即可使用

迭代器不是const迭代器。


john


When you iterate through a list of objects in a list.

list<object> mylist;
list<object>::const_iterator iter;
object ob;

for (iter=mylist.begin(); iter != mylist.end(); ++iter)
{
ob = *iter;
ob.value = 10;
}

So here I am iterating through the list
but is the statement ob = *iter making a copy of an element in my list?
or am I getting a refrence to an element in mylist?
It seems inefficient to me to make a copy of an element in the list, when
all I really want is a pointer to the object or a refrence to it so that
I can modify it...

I assume that iter->value is not going to work...

Comments?

解决方案

JustSomeGuy wrote:

When you iterate through a list of objects in a list.

list<object> mylist;
list<object>::const_iterator iter;
object ob;

for (iter=mylist.begin(); iter != mylist.end(); ++iter)
{
ob = *iter;
You get a copy;
ob.value = 10;
You modify the copy. Now, you probably want to insert:

*iter = ob;
}

So here I am iterating through the list
but is the statement ob = *iter making a copy of an element in my list?
or am I getting a refrence to an element in mylist?

It seems inefficient to me to make a copy of an element in the list, when
all I really want is a pointer to the object or a refrence to it so that
I can modify it...
Yep, that would be inefficient.

I assume that iter->value is not going to work...



You did not actually try, did you?
Best

Kai-Uwe



"JustSomeGuy" <no**@nottelling.com> wrote in message
news:DmtAc.757759


Pk3.612408@pd7tw1no...

When you iterate through a list of objects in a list.

list<object> mylist;
list<object>::const_iterator iter;
object ob;

for (iter=mylist.begin(); iter != mylist.end(); ++iter)
{
ob = *iter;
ob.value = 10;
}

So here I am iterating through the list
but is the statement ob = *iter making a copy of an element in my list?
or am I getting a refrence to an element in mylist?
It seems inefficient to me to make a copy of an element in the list, when
all I really want is a pointer to the object or a refrence to it so that
I can modify it...

I assume that iter->value is not going to work...

Comments?



Curious, if you want to modify the list why did you use a const_iterator? If
you want a pointer or reference to an element in the list, why didn''t you
just declare one?

list<object>::iterator iter;
object& ref;
object* ptr;

for (iter=mylist.begin(); iter != mylist.end(); ++iter)
{
ref = *iter;
ptr = &*iter;
ref.value = 10;
ptr->value = 10;

But as Kai-Uwe said simply ''iter->value = 10'' works provided you use an
iterator not a const iterator.

john


这篇关于关于std :: list的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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