When you iterate through a list of objects in a list.
list<object> mylist;
list<object>::c onst_iterator iter;
object ob;
for (iter=mylist.be gin(); 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? 21 2106
JustSomeGuy wrote: When you iterate through a list of objects in a list.
list<object> mylist; list<object>::c onst_iterator iter; object ob;
for (iter=mylist.be gin(); 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
"JustSomeGu y" <no**@nottellin g.com> wrote in message
news:DmtAc.7577 59$Pk3.612408@p d7tw1no... When you iterate through a list of objects in a list.
list<object> mylist; list<object>::c onst_iterator iter; object ob;
for (iter=mylist.be gin(); 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>::i terator iter;
object& ref;
object* ptr;
for (iter=mylist.be gin(); 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
On Fri, 18 Jun 2004 06:53:44 +0100 in comp.lang.c++, "John Harrison"
<jo************ *@hotmail.com> wrote, object& ref;
References must be initialized to refer to an object.
ref = *iter;
References may not be reseated after initialization.
Assignment assigns to the underlying object.
for (iter=mylist.be gin(); iter != mylist.end(); ++iter)
{
object &ref(*iter);
ref.value = 10;
}
"David Harmon" <so****@netcom. com.invalid> wrote in message
news:40******** *******@news.we st.earthlink.ne t... On Fri, 18 Jun 2004 06:53:44 +0100 in comp.lang.c++, "John Harrison" <jo************ *@hotmail.com> wrote, object& ref;
References must be initialized to refer to an object.
ref = *iter;
References may not be reseated after initialization. Assignment assigns to the underlying object.
for (iter=mylist.be gin(); iter != mylist.end(); ++iter) { object &ref(*iter); ref.value = 10; }
Yes, my mistake.
john
John Harrison wrote: "David Harmon" <so****@netcom. com.invalid> wrote in message news:40******** *******@news.we st.earthlink.ne t... On Fri, 18 Jun 2004 06:53:44 +0100 in comp.lang.c++, "John Harrison" <jo************ *@hotmail.com> wrote, object& ref;
References must be initialized to refer to an object.
ref = *iter;
References may not be reseated after initialization. Assignment assigns to the underlying object.
for (iter=mylist.be gin(); iter != mylist.end(); ++iter) { object &ref(*iter); ref.value = 10; }
Yes, my mistake.
john
Is there any speed difference between the refrence vs the pointer?
John Harrison wrote: 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?
At the risk of sounding like a novice... uhmmm... I didn't know there was
any other type of iterator but a const_iterator. .. doh! :)
> 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?
this doesn't want to compile....
class image : public std::list<eleme nt>
{
element getElement(key k) const
{
image::iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
element &elem(*iter) ;
if (key == k)
{
return(elem);
}
}
throw("Not found");
}
I think the reason this isn't working is due to the const declaration of the
method.
So what am I to do?
On Fri, 18 Jun 2004 11:26:12 -0600 in comp.lang.c++, JustSomeGuy
<No***@ucalgary .ca> wrote, Is there any speed difference between the refrence vs the pointer?
Probably none. Only actual measurement can tell for sure.
"JustSomeGu y" <No***@ucalgary .ca> wrote in message
news:40******** *******@ucalgar y.ca... 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?
this doesn't want to compile....
class image : public std::list<eleme nt> { element getElement(key k) const { image::iterator iter; for (iter=begin(); iter != end(); ++iter) { element &elem(*iter) ; if (key == k) { return(elem); } } throw("Not found"); }
I think the reason this isn't working is due to the const declaration of
the method. So what am I to do?
const iterators and const references
class image : public std::list<eleme nt>
{
element getElement(key k) const
{
image::const_it erator iter;
for (iter=begin(); iter != end(); ++iter)
{
const element &elem(*iter) ;
if (key == k)
{
return(elem);
}
}
throw("Not found");
}
That's the thing about const, it's contagious. Start using it one place and
it ends up everywhere.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl list class must Ioverride in order for this
to work?
|
by: JustSomeGuy |
last post by:
Can you access elements of the std::list like they were an array?
eg.
std::list<int> xlist;
int y;
....
y = xlist; // Returns the 10th element of a list.
|
by: Eric Lilja |
last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope
it's readable at least):
#include <algorithm>
#include <iostream>
#include <vector>
class Row
{
public:
|
by: Josh Mcfarlane |
last post by:
Just out of curiosity:
When would using std::list be more efficient / effective than using
other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be specialized for
anything.
Thanks,
Josh McFarlane
|
by: alex221 |
last post by:
In need to implement a tree structure in which every node has arbitrary
number of children the following code has come into mind:
using std::list;
template < class Contents class Tree_node{
Contents cn;
list < BTree_node < Contents children;
........
| |
by: SpreadTooThin |
last post by:
I want to replace an object in a list with a new object....
std::list<myObj>::iterator it;
for (it=mylist.begin(); it != mylist.end(); ++it)
{
if (it->compair(myval) == 0)
{
*it = newval;
}
|
by: Spoon |
last post by:
Hello,
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
#include <list>
typedef std::list < int foo_t;
int main()
{
int v = { 12, 34 };
|
by: Javier |
last post by:
Hi all,
I have this code:
class A
{
std::list<Bm_observadores;
void function()
{
|
by: isliguezze |
last post by:
template <class T>
class List {
public:
List();
List(const List&);
List(int, const T&);
void push_back(const T &);
void push_front(const T &);
void pop_back();
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |