iterators

S

slurper

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
 
V

Victor Bazarov

slurper said:
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.
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));
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
 
J

Jeff Flinn

slurper said:
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
 
R

Richard Herring

slurper said:
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.
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.
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<job>::iterator> joblist;

(And remember that you must not perform any operations on the vector
which will invalidate the iterators.)
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top