move vector into list

L

levent

Hi,

Is it possible (with STL) to move the contents of a vector into a list?


Obviously this would be a linear complexity operation (number of
elements in vector), but is it possible to do this w/o copy
constructing every element into list and destructing the vector
contents?

Or, if you think this is a bad question, why is it so? (comments on
this would really help)
 
V

Victor Bazarov

levent said:
Is it possible (with STL) to move the contents of a vector into a
list?

Don't you mean, "copy"? You can either copy using 'std::copy' function
or by initialising your list from the pair of iterators.
Obviously this would be a linear complexity operation (number of
elements in vector), but is it possible to do this w/o copy
constructing every element into list and destructing the vector
contents?

No, it is not, without relying heavily on the implementation details
of both containers. Standard list container has very specific data
requirement: all its elements are independent and should be able to
be destroyed separately. Even if you manage to pass addresses of the
vector elements to the list nodes, as soon as those elements will be
removed from the list, you will get in trouble because a vector keeps
its data in a dynamic array (most likely), and in a dynamic array all
elements are interdependent and cannot be deallocated separately.
Or, if you think this is a bad question, why is it so? (comments on
this would really help)

This not not a bad question.

V
 
L

levent

Thanks for the answer.

I actually meant move. I understand this problem of simultaneously
associating an element with multiple containers. So the question was
how to let vector `forget' about its elements without actually
delete[]ing them, and pass the `responsibility' to list. The effect
would be similar to the `splice' member function of the list container
called on vector as follows:

assert(mylist.size() == M); assert(myvector.size() == N);
mylist.splice(mylist.end(), myvector);
assert(mylist.size() == M+N); assert(myvector.size() == 0);


Now, one of the reasons I thought this might be bad question is that I
am not sure if it is possible to do this at all even by hand. For
instance is it at all possible to allocate a chunk of data with new
T[N], then delete them one by one as if each one of N elements were
separately allocated?
 
V

Victor Bazarov

levent said:
I actually meant move. I understand this problem of simultaneously
associating an element with multiple containers. So the question was
how to let vector `forget' about its elements without actually
delete[]ing them, and pass the `responsibility' to list. The effect
would be similar to the `splice' member function of the list container
called on vector as follows:

assert(mylist.size() == M); assert(myvector.size() == N);
mylist.splice(mylist.end(), myvector);
assert(mylist.size() == M+N); assert(myvector.size() == 0);


Now, one of the reasons I thought this might be bad question is that I
am not sure if it is possible to do this at all even by hand. For
instance is it at all possible to allocate a chunk of data with new
T[N], then delete them one by one as if each one of N elements were
separately allocated?
 
V

Victor Bazarov

levent said:
I actually meant move. I understand this problem of simultaneously
associating an element with multiple containers. So the question was
how to let vector `forget' about its elements without actually
delete[]ing them, and pass the `responsibility' to list. The effect
would be similar to the `splice' member function of the list container
called on vector as follows:

assert(mylist.size() == M); assert(myvector.size() == N);
mylist.splice(mylist.end(), myvector);
assert(mylist.size() == M+N); assert(myvector.size() == 0);


Now, one of the reasons I thought this might be bad question is that I
am not sure if it is possible to do this at all even by hand. For
instance is it at all possible to allocate a chunk of data with new
T[N], then delete them one by one as if each one of N elements were
separately allocated?

No. That's the reason why what you asked about cannot be done. It
does not make your question bad, mind you.

V
 
V

Victor Bazarov

Victor said:
levent said:
I actually meant move. I understand this problem of simultaneously
associating an element with multiple containers. So the question was
how to let vector `forget' about its elements without actually
delete[]ing them, and pass the `responsibility' to list. The effect
would be similar to the `splice' member function of the list
container called on vector as follows:

assert(mylist.size() == M); assert(myvector.size() == N);
mylist.splice(mylist.end(), myvector);
assert(mylist.size() == M+N); assert(myvector.size() == 0);


Now, one of the reasons I thought this might be bad question is that
I am not sure if it is possible to do this at all even by hand. For
instance is it at all possible to allocate a chunk of data with new
T[N], then delete them one by one as if each one of N elements were
separately allocated?

No.

Let me rephrase that. It can be done with a custom allocator. And
that's probably how it should be done; however, that is not something
I would even waste *my* time on. When a project requires a kludge
like this to simply replace one container with another, a redesign of
the project is in order rather than an implementation of the kludge.
That's the reason why what you asked about cannot be done. It
does not make your question bad, mind you.

V
 

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
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top