C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ list

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
steffi
Guest





PostPosted: Sat Sep 11, 2004 5:01 am    Post subject: C++ list Reply with quote



Hi,

I have the following problem: I want to pick some elements of a list,
save the references in another one and process the 2nd list. I want
the changes that I made on elements in the 2nd list to affect the
corresponding elements in the first list......

This is part of my code, but when I change the contents of the
elements in list2 nothing happens with the elements in list1.......

list<int> list1;
list<int> list2;

......initialize list1 with int-elements........

for(list<int>::iterator it = list1.begin(); it != list1.end(); it++)
{ int &element = *it;
list2.push_back(element);
}

for(list<int>::iterator it = list2.begin(); it != list2.end(); it++)
{ *it = (*it) + 2;
}


cheers
steffi

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Sun Sep 12, 2004 10:18 am    Post subject: Re: C++ list Reply with quote



steffi <chupchup (AT) web (DOT) de> wrote:

Quote:
Hi,

I have the following problem: I want to pick some elements of a list,
save the references in another one and process the 2nd list. I want
the changes that I made on elements in the 2nd list to affect the
corresponding elements in the first list......

This is part of my code, but when I change the contents of the
elements in list2 nothing happens with the elements in list1.......

list<int> list1;
list<int> list2;

.....initialize list1 with int-elements........

for(list<int>::iterator it = list1.begin(); it != list1.end(); it++)
{ int &element = *it;
list2.push_back(element);
}
This makes two independent lists and is equivalent to list2 = list1;

Todo what you want I'd suggest a list of list<int>::iterator's. For
example
list<list list2;
for(list<int>::iiterator it = list1.begin();it != list1.end();++it)
list2.push_back<it>;

Quote:

for(list<int>::iterator it = list2.begin(); it != list2.end(); it++)
{ *it = (*it) + 2;
}
for(list<list::iterator it =

list2.begin;it!=list2.end();++it)
**it += 2;

see also boost::rwf to make the second list a list of refs...



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jeff Flinn
Guest





PostPosted: Mon Sep 13, 2004 10:40 am    Post subject: Re: C++ list Reply with quote




"steffi" <chupchup (AT) web (DOT) de> wrote

Quote:
Hi,

I have the following problem: I want to pick some elements of a list,
save the references in another one and process the 2nd list. I want
the changes that I made on elements in the 2nd list to affect the
corresponding elements in the first list......

This is part of my code, but when I change the contents of the
elements in list2 nothing happens with the elements in list1.......

list<int> list1;
list<int> list2;

.....initialize list1 with int-elements........

for(list<int>::iterator it = list1.begin(); it != list1.end(); it++)
{ int &element = *it;
list2.push_back(element);
}

for(list<int>::iterator it = list2.begin(); it != list2.end(); it++)
{ *it = (*it) + 2;
}

see www.boost.org

typedef boost::shared_ptr<int> tIntPtr;
typedef std::list<tIntPtr> tIntPtrs;
typedef tIntPtrs::iterator tItr;

tIntPtrs list1;

.... intitial list1

tIntPtrs list2( list1 );

for( tItr lItr = list2.begin() ; lItr != list2.end() ; ++lItr )
{
**lItr += 2;
}

// you can use boost iterator adaptors as well

Jeff F



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Maxim Yegorushkin
Guest





PostPosted: Tue Sep 14, 2004 10:48 pm    Post subject: Re: C++ list Reply with quote

Jeff Flinn <flinn (AT) adi (DOT) com> wrote:

Quote:
see www.boost.org

typedef boost::shared_ptr<int> tIntPtr;
typedef std::list<tIntPtr> tIntPtrs;
typedef tIntPtrs::iterator tItr;

boost::shared_ptr<> seems an overkill here. All original poster needs is a
list of references, not pointers, which naturally is
std::list<boost::reference_wrapper. Using boost::shared_ptr<> you
get +1 useless memory allocation for each element.

--
Maxim Yegorushkin

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jeff Flinn
Guest





PostPosted: Wed Sep 15, 2004 6:39 pm    Post subject: Re: C++ list Reply with quote


"Maxim Yegorushkin" <e-maxim (AT) yandex (DOT) ru> wrote

Quote:
Jeff Flinn <flinn (AT) adi (DOT) com> wrote:

see www.boost.org

typedef boost::shared_ptr<int> tIntPtr;
typedef std::list<tIntPtr> tIntPtrs;
typedef tIntPtrs::iterator tItr;

boost::shared_ptr<> seems an overkill here. All original poster needs is a
list of references, not pointers, which naturally is
std::list<boost::reference_wrapper. Using boost::shared_ptr<> you
get +1 useless memory allocation for each element.

Excellent! As long as the OP ensures the referents outlive the references.

Jeff



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Thu Sep 16, 2004 11:03 am    Post subject: Re: C++ list Reply with quote

[email]chupchup (AT) web (DOT) de[/email] (steffi) wrote in message news:<f3fbd21b.0409101016.7260459b (AT) posting (DOT) google.com>...
Quote:
...
This is part of my code, but when I change the contents of the
elements in list2 nothing happens with the elements in list1.......

list<int> list1;
list<int> list2;

.....initialize list1 with int-elements........

for(list<int>::iterator it = list1.begin(); it != list1.end(); it++)
{ int &element = *it;
list2.push_back(element);
}

for(list<int>::iterator it = list2.begin(); it != list2.end(); it++)
{ *it = (*it) + 2;
}
...

(1)
list<int> list1;
list<int> list2;

This is the original code. As integer elements of the list2 are value
copies of elements of the list1, the list2 elements do not link to the
list1 elements, even if he has tried the code below:
for(list<int>::iterator it = list1.begin(); it != list1.end(); it++)
{
int &element = *it;
list2.push_back(element); // value copy
}

(2)
list<int> list1;
list<int&> list2; error for allocater

Probably, this is what he wants to do, but unfortunately this is ill
formed to cause an allocation error. You cannot allocate a reference
in (heap) memory.

(3)
list<int> list1;
list<list*> list2;

I wander why this simple solution has not been proposed up to now.
This is the most simple and the most memory saving solution. What
problems does this have?

(4)
list<int> list1;
list< list list3;

This is proposed by Carl Barron.

(5)
list<int> list1;
list< boost::reference_wrapper list4;

This proposed by Maxim Yegorushkin.

(6)
list<boost::shared_ptr list5;
list<boost::shared_ptr list6;

This is proposed by Jeff Flinn.

I presented below a workable code for each. Which do you like best?
Sorry for an extra {} over for()... statement. This is only for my
MSVC++6.

#include "stdafx.h"
#include <list>
#include <iterator>
#include <iostream>
#include <boost/ref.hpp>
#include <boost/smart_ptr.hpp>
using namespace std;

list<int> list1;

//list<int&> list2; // error for allocator
list<int*> list2;
//list<int*const> list2; // error for MSVC++6, ok for MSVC++7.1

list<list list3;

list<boost::reference_wrapper list4;

typedef boost::shared_ptr<int> Ptr;
list<Ptr> list5;

// for output of list5. You can use boost::lambda instread of f()
void f(Ptr p) {
std::cout << *p << " ";
}

int main(int argc, char* argv[])
{
//.....initialize list1 with int-elements........
list1.push_back(0); list1.push_back(1); list1.push_back(2);


//// list2
{for(list {
list2.push_back(&(*it));
}}

{for(list<int*>::iterator it = list2.begin(); it != list2.end();
it++)
{
**it += 2;
}}

copy (list1.begin(), list1.end(), ostream_iterator<int>(cout, " "));
cout << endl;

//// list3
{for(list {
list3.push_back(it);
}}

{for(list<list::iterator it = list3.begin();
it != list3.end(); it++)
{
**it += 2;
}}

copy (list1.begin(), list1.end(), ostream_iterator<int>(cout, " "));
cout << endl;

//// list4
{for(list {
list4.push_back(boost::reference_wrapper<int>(*it));
}}

{for(list<boost::reference_wrapper::iterator it =
list4.begin(); it != list4.end(); it++)
{
*it += 2;
}}

copy (list1.begin(), list1.end(), ostream_iterator<int>(cout, " "));
cout << endl;

//// list5, list6
//.....initialize list5 with shared pointer to int-elements........
list5.push_back(Ptr(new int(0))); list5.push_back(Ptr(new int(1)));
list5.push_back(Ptr(new int(2)));

list< Ptr > list6(list5);

{for(list<Ptr>::iterator it = list6.begin(); it != list6.end(); it++)
{
**it += 2;
}}

for_each (list5.begin(), list5.end(), f);
cout << endl;


return 0;
}

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jeff Flinn
Guest





PostPosted: Fri Sep 17, 2004 3:03 am    Post subject: Re: C++ list Reply with quote


"Tokyo Tomy" <hosoda (AT) jtec (DOT) or.jp> wrote

Quote:
chupchup (AT) web (DOT) de (steffi) wrote in message
news:<f3fbd21b.0409101016.7260459b (AT) posting (DOT) google.com>...


....

Quote:

I presented below a workable code for each. Which do you like best?
Sorry for an extra {} over for()... statement. This is only for my
MSVC++6.

You can avoid the {for(){}} with:

#define for if(0);else for

Jeff F



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tokyo Tomy
Guest





PostPosted: Sat Sep 18, 2004 8:03 pm    Post subject: Re: C++ list Reply with quote

"Jeff Flinn" <flinn (AT) adi (DOT) com> wrote

Quote:
"Tokyo Tomy" <hosoda (AT) jtec (DOT) or.jp> wrote in message
news:49c1da0b.0409160114.2926822f (AT) posting (DOT) google.com...
[email]chupchup (AT) web (DOT) de[/email] (steffi) wrote in message
news:<f3fbd21b.0409101016.7260459b (AT) posting (DOT) google.com>...

...


I presented below a workable code for each. Which do you like best?
Sorry for an extra {} over for()... statement. This is only for my
MSVC++6.

You can avoid the {for(){}} with:

#define for if(0);else for


Thank you for your advice, Jeff Flinn.

My MSVC++6 respects a tradition and recognize the scope of a value "i"
in "for( int i = ….){ }" as outside the scope of "for( ){ }". MSVC++
7.1 does not respect the tradition.

I am sure that the extra "{ }" is not harmful for any other compilers.
I should have done Jeff Flinn said, but let me continue to use the
extra "{ }" over "for( ){ }.

Apart from the advice, let me correct a clear error in my previous
post as follows:

Quote:
(3)
list<int> list1;
list<list*> list2;

should be:
(3)
list<int> list1;
list<int*> list2; // correction in < >

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Mon Sep 20, 2004 7:05 pm    Post subject: Re: C++ list Reply with quote

"Jeff Flinn" <flinn (AT) adi (DOT) com> wrote

Quote:
"Tokyo Tomy" <hosoda (AT) jtec (DOT) or.jp> wrote in message
news:49c1da0b.0409160114.2926822f (AT) posting (DOT) google.com...
[email]chupchup (AT) web (DOT) de[/email] (steffi) wrote in message
news:<f3fbd21b.0409101016.7260459b (AT) posting (DOT) google.com>...

...

I presented below a workable code for each. Which do you like best?
Sorry for an extra {} over for()... statement. This is only for my
MSVC++6.

You can avoid the {for(){}} with:

#define for if(0);else for

A cleaner solution is just to use different names for the variables.

The cleanest solution is probably to put the loops in different
functions.

--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.