STL vector with iterators

J

Jan.Steinke

Hi there,
i've got a STL list like this:
list<object *> myList;
Now, i want to create a vector with iterators of this list like this:

vector< list<object *>::iterator > myVector

Where's the fault?

thanks for reply,
Jan
 
?

=?ISO-8859-1?Q?Stefan_N=E4we?=

Hi there,
i've got a STL list like this:
list<object *> myList;
Now, i want to create a vector with iterators of this list like this:

vector< list<object *>::iterator > myVector

Where's the fault?

Where's the code ?

S.
 
G

Gianni Mariani

Hi there,
i've got a STL list like this:
list<object *> myList;
Now, i want to create a vector with iterators of this list like this:

vector< list<object *>::iterator > myVector

Where's the fault?

That's ok. If you're using it in a template, it won't work. You'll
need this:

vector< typename list<object *>::iterator > myVector

You may also need to worry about const-ness.

vector< typename list<object *>::const_iterator > myVector
 
S

Stephane Wirtel

(e-mail address removed) said the following on 20/01/2006 12:51:
Hi there,
i've got a STL list like this:
list<object *> myList;
Now, i want to create a vector with iterators of this list like this:

vector< list<object *>::iterator > myVector

Can you try this template class ?

template< typename T > class MyVector
: public std::vector< typename std::list <T *> :: iterator > {
};
 
A

Axter

Hi there,
i've got a STL list like this:
list<object *> myList;
Now, i want to create a vector with iterators of this list like this:

vector< list<object *>::iterator > myVector

Where's the fault?

thanks for reply,
Jan

I recommend not using raw pointers in your container, and instead use
smart pointers like boost::shared_ptr or smart_ptr
http://code.axter.com/smart_ptr.h
Other pointers:
http://code.axter.com/cow_ptr.h
http://code.axter.com/copy_ptr.h

Are you sure you need the list container in the first place. It's
rarely optimal to use std::list over std::vector/std::deque.
If you're not doing a lot of insertions and deletions in the center of
the container, you should consider just using std::vector in the first
place.
I recommend you do some performance test to see if you really need the
std::list in the first place.
 
R

roberts.noah

Gianni said:
That's ok. If you're using it in a template, it won't work. You'll
need this:

vector< typename list<object *>::iterator > myVector

You may also need to worry about const-ness.

vector< typename list<object *>::const_iterator > myVector

When and why you need 'typename' is explained pretty well in Thinking
in C++ V2, which is available for download on the internet.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top