473,382 Members | 1,635 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

A Question about std::list

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?
Jul 22 '05 #1
21 2063
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
Jul 22 '05 #2

"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
Jul 22 '05 #3
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.begin(); iter != mylist.end(); ++iter)
{
object &ref(*iter);
ref.value = 10;
}

Jul 22 '05 #4

"David Harmon" <so****@netcom.com.invalid> wrote in message
news:40***************@news.west.earthlink.net...
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.begin(); iter != mylist.end(); ++iter)
{
object &ref(*iter);
ref.value = 10;
}


Yes, my mistake.

john
Jul 22 '05 #5
John Harrison wrote:
"David Harmon" <so****@netcom.com.invalid> wrote in message
news:40***************@news.west.earthlink.net...
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.begin(); 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?
Jul 22 '05 #6
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! :)
Jul 22 '05 #7
>
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<element>
{
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?
Jul 22 '05 #8
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.

Jul 22 '05 #9

"JustSomeGuy" <No***@ucalgary.ca> wrote in message
news:40***************@ucalgary.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<element>
{
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<element>
{
element getElement(key k) const
{
image::const_iterator 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
Jul 22 '05 #10
[snip OPs post..]

John Harrison wrote:
That's the thing about const, it's contagious. Start using it one place and
it ends up everywhere.

which is also the nice thing about const, IMHO..

rlc
Jul 22 '05 #11
David Harmon <so****@netcom.com.invalid> wrote in message news:<40***************@news.west.earthlink.net>.. .
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.


Could you please explain why?
References may not be reseated after initialization.
Assignment assigns to the underlying object.


What does it mean?

Thanks a lot.

John
Jul 22 '05 #12

"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
David Harmon <so****@netcom.com.invalid> wrote in message news:<40***************@news.west.earthlink.net>.. .
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.


Could you please explain why?


Because that's what the language is. An uninitialised reference would be
completely useless (see below).
References may not be reseated after initialization.
Assignment assigns to the underlying object.


What does it mean?


int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a
r = 3; // now a equals 3
r = b; // what does this do?

There are two ways of interpreting 'r = b', you could say that it makes r
refer to b, or you could say that is assigns the value of b to a (because r
refers to a). The latter is correct, that is what people mean when they say
references cannot be reseated.

john
Jul 22 '05 #13
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2j*************@uni-berlin.de>...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
David Harmon <so****@netcom.com.invalid> wrote in message

news:<40***************@news.west.earthlink.net>.. .
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.


Could you please explain why?


Because that's what the language is. An uninitialised reference would be
completely useless (see below).
References may not be reseated after initialization.
Assignment assigns to the underlying object.


What does it mean?


int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a
r = 3; // now a equals 3
r = b; // what does this do?

There are two ways of interpreting 'r = b', you could say that it makes r
refer to b, or you could say that is assigns the value of b to a (because r
refers to a). The latter is correct, that is what people mean when they say
references cannot be reseated.

john


Thanks a lot.
Can I do this?

int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a
r = 3; // now a equals 3
r = &b; // Is this correct?

John
Jul 22 '05 #14
In message <c3**************************@posting.google.com >, John
<jo*********@yahoo.com> writes
"John Harrison" <jo*************@hotmail.com> wrote in message
news:<2j*************@uni-berlin.de>...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
> David Harmon <so****@netcom.com.invalid> wrote in message news:<40***************@news.west.earthlink.net>.. .
> > 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.
>
> Could you please explain why?


Because that's what the language is. An uninitialised reference would be
completely useless (see below).
>
> > References may not be reseated after initialization.
> > Assignment assigns to the underlying object.
> >
>
> What does it mean?


int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a
r = 3; // now a equals 3
r = b; // what does this do?

There are two ways of interpreting 'r = b', you could say that it makes r
refer to b, or you could say that is assigns the value of b to a (because r
refers to a). The latter is correct, that is what people mean when they say
references cannot be reseated.


Thanks a lot.
Can I do this?

int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a


From now on, r "is" a, and this association can't be changed.
r = 3; // now a equals 3
r = &b; // Is this correct?


No. Anything beginning "r =" is an assignment to what r references,
regardless of what is on the right of the = sign. It *never* changes the
association between r and a.

So you're attempting to store the address of b in what r references,
which is a. Since a isn't a pointer-to-int, you can't.

--
Richard Herring
Jul 22 '05 #15
"John Harrison" <jo*************@hotmail.com> wrote:
const iterators and const references

class image : public std::list<element>
{
element getElement(key k) const
'key' must be a typename, if this is to compile...
{
image::const_iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
const element &elem(*iter);
if (key == k)
.... so this won't
{
return(elem);
}
}
throw("Not found");
}

That's the thing about const, it's contagious. Start using it one place and
it ends up everywhere.


Or was this meant to be pseudocode?
Jul 22 '05 #16

"Old Wolf" <ol*****@inspire.net.nz> wrote in message
news:84**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote:
const iterators and const references

class image : public std::list<element>
{
element getElement(key k) const


'key' must be a typename, if this is to compile...
{
image::const_iterator iter;
for (iter=begin(); iter != end(); ++iter)
{
const element &elem(*iter);
if (key == k)


... so this won't
{
return(elem);
}
}
throw("Not found");
}

That's the thing about const, it's contagious. Start using it one place and it ends up everywhere.


Or was this meant to be pseudocode?

Yes it was code written directly into the news poster not cut and past from
a compiler.
There is an error here where key was meant to be iter->key or iter.key

Jul 22 '05 #17
Richard Herring <ju**@[127.0.0.1]> wrote in message news:<VO**************@baesystems.com>...
In message <c3**************************@posting.google.com >, John
<jo*********@yahoo.com> writes
"John Harrison" <jo*************@hotmail.com> wrote in message
news:<2j*************@uni-berlin.de>...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
> David Harmon <so****@netcom.com.invalid> wrote in message news:<40***************@news.west.earthlink.net>.. . > > 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.
>
> Could you please explain why?

Because that's what the language is. An uninitialised reference would be
completely useless (see below).

>
> > References may not be reseated after initialization.
> > Assignment assigns to the underlying object.
> >
>
> What does it mean?

int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a
r = 3; // now a equals 3
r = b; // what does this do?

There are two ways of interpreting 'r = b', you could say that it makes r
refer to b, or you could say that is assigns the value of b to a (because r
refers to a). The latter is correct, that is what people mean when they say
references cannot be reseated.


Thanks a lot.
Can I do this?

int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a


From now on, r "is" a, and this association can't be changed.
r = 3; // now a equals 3
r = &b; // Is this correct?


No. Anything beginning "r =" is an assignment to what r references,
regardless of what is on the right of the = sign. It *never* changes the
association between r and a.

So you're attempting to store the address of b in what r references,
which is a. Since a isn't a pointer-to-int, you can't.


Thanks. How about call-by-reference?
Below is an example:

void function1(){

std::vector <int*> v1;
.....
for (int i = 0; i < 10; i++){
function2(v1); //function2 is called to bring value back to v1.
//process v1.
.....
}
}

void function2(std::vector <int*> &v2){
//Assign value to v2.
.....
}

Is there any problem with above code?

Thanks a lot.

John.
Jul 22 '05 #18
Richard Herring <ju**@[127.0.0.1]> wrote in message news:<VO**************@baesystems.com>...
In message <c3**************************@posting.google.com >, John
<jo*********@yahoo.com> writes
"John Harrison" <jo*************@hotmail.com> wrote in message
news:<2j*************@uni-berlin.de>...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
> David Harmon <so****@netcom.com.invalid> wrote in message news:<40***************@news.west.earthlink.net>.. . > > 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.
>
> Could you please explain why?

Because that's what the language is. An uninitialised reference would be
completely useless (see below).

>
> > References may not be reseated after initialization.
> > Assignment assigns to the underlying object.
> >
>
> What does it mean?

int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a
r = 3; // now a equals 3
r = b; // what does this do?

There are two ways of interpreting 'r = b', you could say that it makes r
refer to b, or you could say that is assigns the value of b to a (because r
refers to a). The latter is correct, that is what people mean when they say
references cannot be reseated.


Thanks a lot.
Can I do this?

int a = 1;
int b = 2;
int& r = a; // r initialised to refer to a


From now on, r "is" a, and this association can't be changed.
r = 3; // now a equals 3
r = &b; // Is this correct?


No. Anything beginning "r =" is an assignment to what r references,
regardless of what is on the right of the = sign. It *never* changes the
association between r and a.

So you're attempting to store the address of b in what r references,
which is a. Since a isn't a pointer-to-int, you can't.

Thanks. How about call-by-reference?
Below is an example:

void function1(){

std::vector <int*> v1;
.....
for (int i = 0; i < 10; i++){
function2(v1); //function2 is called to bring value back to v1.
//process v1.
.....
}
}

void function2(std::vector <int*> &v2){
v2.clear(); //Erase old value stored in v1. Is this correct?
//Assign value to v2.
.....
}

Is there any problem with above code?

Thanks a lot.

John.

( I add one line to function2 of my OP. So I post it again.)
Jul 22 '05 #19
>
Thanks. How about call-by-reference?
Below is an example:

void function1(){

std::vector <int*> v1;
.....
for (int i = 0; i < 10; i++){
function2(v1); //function2 is called to bring value back to v1.
//process v1.
.....
}
}

void function2(std::vector <int*> &v2){
v2.clear(); //Erase old value stored in v1. Is this correct?
//Assign value to v2.
.....
}

Is there any problem with above code?


It's fine, why shouldn't it be? Do you think it contradicts something that
Richard or myself have already said? If so what? The rule about not being
able to reseat a reference when it has been initialised still applies.
Confused.

john
Jul 22 '05 #20
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2k*************@uni-berlin.de>...

Thanks. How about call-by-reference?
Below is an example:

void function1(){

std::vector <int*> v1;
.....
for (int i = 0; i < 10; i++){
function2(v1); //function2 is called to bring value back to v1.
//process v1.
.....
}
}

void function2(std::vector <int*> &v2){
v2.clear(); //Erase old value stored in v1. Is this correct?
//Assign value to v2.
.....
}

Is there any problem with above code?


It's fine, why shouldn't it be? Do you think it contradicts something that
Richard or myself have already said? If so what? The rule about not being
able to reseat a reference when it has been initialised still applies.
Confused.

john


Thanks.
Each time the for loop in function1 is executed, a new v2 is declared
and refers to v1, right?
By the way, in function2, is the line-- v2.clear(), necessary?

Thanks again.

John
Jul 22 '05 #21

"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2k*************@uni-berlin.de>...

Thanks. How about call-by-reference?
Below is an example:

void function1(){

std::vector <int*> v1;
.....
for (int i = 0; i < 10; i++){
function2(v1); //function2 is called to bring value back to v1. //process v1.
.....
}
}

void function2(std::vector <int*> &v2){
v2.clear(); //Erase old value stored in v1. Is this correct?
//Assign value to v2.
.....
}

Is there any problem with above code?


It's fine, why shouldn't it be? Do you think it contradicts something that Richard or myself have already said? If so what? The rule about not being able to reseat a reference when it has been initialised still applies.
Confused.

john


Thanks.
Each time the for loop in function1 is executed, a new v2 is declared
and refers to v1, right?


Right, except I would say created not declared, v2 is declared once only.
Each different time that v2 is created it can refer to a different object,
but within the same creation it always refers to the same object.
By the way, in function2, is the line-- v2.clear(), necessary?


If your function depends on the fact that v2 must refer to an empty vector
then it's good practice to empty it at the start of function rather then
relying on the caller of the function to always call with an empty vector.
On the other hand if all you are going to do is assign a whole new vector to
v2 (which is what your comments suggest) then it isn't necessary.

john
Jul 22 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
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...
5
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.
5
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:
44
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...
7
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{ ...
7
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;...
8
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...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
12
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();
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.