 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
slurper Guest
|
Posted: Fri Oct 22, 2004 3:42 pm Post subject: iterators |
|
|
i have problems with following piece of code
vector<job> jobvector; (i put some jobs in this vector)
list<job*> joblist;
void copy_vector_into_list() {
vector<job>::iterator iter = jobvector.begin();
for ( iter; iter != jobvector.end(); ++iter ) {
joblist.push_back(iter);
}
}
what i want:
i have a vector of jobs
i want to add the jobs to the list also, but to avoid a copy of the job, i
want to use pointers to jobs, so that no copies for jobs need to be made.
but there seems to be a problem
iter is vector<job>::iterator. i want to do a push_back on the list of the
job pointed to by the iterator. how should i do this?? casting? but how?
i make the copy (of pointers) because i want to manipulate the list and keep
the vector.
tx for help
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Fri Oct 22, 2004 3:55 pm Post subject: Re: iterators |
|
|
slurper wrote:
| Quote: | i have problems with following piece of code
vector<job> jobvector; (i put some jobs in this vector)
list<job*> joblist;
void copy_vector_into_list() {
vector<job>::iterator iter = jobvector.begin();
for ( iter; iter != jobvector.end(); ++iter ) {
joblist.push_back(iter);
}
}
what i want:
i have a vector of jobs
i want to add the jobs to the list also, but to avoid a copy of the job, i
want to use pointers to jobs, so that no copies for jobs need to be made.
|
Bad idea. Addresses of elements of std::vector are probably the most
unreliable thing in C++. The vector's storage can change easily and your
pointers will become invalid.
| Quote: | but there seems to be a problem
iter is vector<job>::iterator. i want to do a push_back on the list of the
job pointed to by the iterator. how should i do this?? casting? but how?
|
No, not casting. The dereferencing operator (operator*) for an iterator
yields a reference to the object. Taking address will give you the
pointer to the object. Iterators are not pointers.
joblist.push_back(&(*iter));
| Quote: | i make the copy (of pointers) because i want to manipulate the list and keep
the vector.
|
Well, that's up to you, of course.
V
|
|
| Back to top |
|
 |
Jeff Flinn Guest
|
Posted: Fri Oct 22, 2004 3:57 pm Post subject: Re: iterators |
|
|
"slurper" <slurper1234 (AT) hotmail (DOT) com> wrote
| Quote: | i have problems with following piece of code
vector<job> jobvector; (i put some jobs in this vector)
list<job*> joblist;
void copy_vector_into_list() {
vector<job>::iterator iter = jobvector.begin();
for ( iter; iter != jobvector.end(); ++iter ) {
joblist.push_back(iter);
|
joblist.push_back(&(*iter));
Beware that these addresses can become invalidated whenever jobs are added
or removed from jobvector. It's better to use smart pointers in both cases.
Using boost::shared_ptr from www.boost.org:
typedef boost::shared_ptr<job> tJobPtr;
std::vector<tjobPtr> jobvector;
std::list<tJobPtr> joblist;
jobvector.push_back( tJobPtr(new Job) );
....
std::copy( jobvector.begin(), jobvector.end(), std::back_inserter(
joblist ) );
Jeff F
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Fri Oct 22, 2004 4:10 pm Post subject: Re: iterators |
|
|
In message <41792ae8$0$22757$ba620e4c (AT) news (DOT) skynet.be>, slurper
<slurper1234 (AT) hotmail (DOT) com> writes
| Quote: | i have problems with following piece of code
vector<job> jobvector; (i put some jobs in this vector)
list<job*> joblist;
void copy_vector_into_list() {
vector<job>::iterator iter = jobvector.begin();
for ( iter; iter != jobvector.end(); ++iter ) {
joblist.push_back(iter);
}
}
what i want:
i have a vector of jobs
i want to add the jobs to the list also, but to avoid a copy of the job, i
want to use pointers to jobs, so that no copies for jobs need to be made.
|
Think about the problem more abstractly. You want to use something which
_behaves like_ a pointer. It doesn't have to _be_ a pointer.
| Quote: |
but there seems to be a problem
iter is vector<job>::iterator. i want to do a push_back on the list of the
job pointed to by the iterator.
|
No, since you don't want to copy the jobs, you want to push_back
something which acts like a pointer.
| Quote: | how should i do this?? casting? but how?
i make the copy (of pointers) because i want to manipulate the list and keep
the vector.
|
So make the list of vector iterators instead of pointers:
list<vector joblist;
(And remember that you must not perform any operations on the vector
which will invalidate the iterators.)
--
Richard Herring
|
|
| Back to top |
|
 |
Evan Carew Guest
|
Posted: Thu Oct 28, 2004 11:30 pm Post subject: Re: iterators |
|
|
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
slurper wrote:
| Quote: | i have problems with following piece of code
vector<job> jobvector; (i put some jobs in this vector)
list<job*> joblist;
void copy_vector_into_list() {
vector<job>::iterator iter = jobvector.begin();
for ( iter; iter != jobvector.end(); ++iter ) {
joblist.push_back(iter);
}
}
what i want:
i have a vector of jobs
i want to add the jobs to the list also, but to avoid a copy of the job, i
want to use pointers to jobs, so that no copies for jobs need to be made.
but there seems to be a problem
iter is vector<job>::iterator. i want to do a push_back on the list of the
job pointed to by the iterator. how should i do this?? casting? but how?
i make the copy (of pointers) because i want to manipulate the list and keep
the vector.
Slurper, |
Also watch out for the memory leak issue when using raw pointers & the
STL. By default, the STL is designed with object ownership in mind. This
ensures that when a container either goes out of scope, or is explicitly
deleted, all contained objects are also cleaned up. In the case of
storing pointers to objects in the containers, you are responsible for
cleanup yourself.
To address this well known problem (feature), a special type of
"pointer" was developed called "smart pointers" (see the boost's
http://boost.org/libs/smart_ptr/smart_ptr.htm for more info). These
pointers, in turn, delete the objects stored on the heap which you don't
want to copy into the container when appropriate.
Evan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFBgYD5oo/Prlj9GScRApswAJ94RPqPoCv9tCgQlT4YTwR2W1P1qQCcCVDn
xVmvx/BPKzg9fThNN4KIMSA=
=eIBi
-----END PGP SIGNATURE-----
|
|
| Back to top |
|
 |
|
|
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
|
|