473,804 Members | 3,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

move vector into list

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)

Apr 30 '06 #1
5 3546
levent wrote:
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
--
Please remove capital As from my address when replying by mail
Apr 30 '06 #2
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.s ize() == M); assert(myvector .size() == N);
mylist.splice(m ylist.end(), myvector);
assert(mylist.s ize() == 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?

Apr 30 '06 #3
levent wrote:
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.s ize() == M); assert(myvector .size() == N);
mylist.splice(m ylist.end(), myvector);
assert(mylist.s ize() == 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?


--
Please remove capital As from my address when replying by mail
Apr 30 '06 #4
levent wrote:
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.s ize() == M); assert(myvector .size() == N);
mylist.splice(m ylist.end(), myvector);
assert(mylist.s ize() == 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
--
Please remove capital As from my address when replying by mail
Apr 30 '06 #5
Victor Bazarov wrote:
levent wrote:
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.s ize() == M); assert(myvector .size() == N);
mylist.splice(m ylist.end(), myvector);
assert(mylist.s ize() == 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
--
Please remove capital As from my address when replying by mail
Apr 30 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3539
by: Roland Bengtsson | last post by:
I have a class Conception and I have this in a vector, it should be: vector<Conception> vek; // vector vector<Conception>::iterator vek; // iterator to vek But what if I want to have pointers to class Conception instead? How can I do that? And how should I write to declare an iterator to this vector?
7
12641
by: William Payne | last post by:
(This post is related to "recent files menu"-post below. If I should have kept this in that thread, I apologise.) Hello, I have a function that adds a std::string to a std::vector. New entries are added at the front (index 0) of the vector. If the vector contains a certain amount of elements, the element at the back is removed when a new one is added. If one tries to add a string already stored in the vector, that string is supposed to...
11
2907
by: Richard Thompson | last post by:
I've got a memory overwrite problem, and it looks as if a vector has been moved, even though I haven't inserted or deleted any elements in it. Is this possible? In other words, are there any circumstances in which the STL will move a vector, or invalidate iterators to elements in the vector, if you don't insert or remove elements? My actual problem seems to be as follows: I have class X, which contains an STL vector. The constructor...
34
4184
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
11
6316
by: sw | last post by:
Hi, Is it possible to insert a class <vec> which has a vector<double> member, into the vector<vec> veclist for e.g? I've been getting compilation errors when trying to insert using the vector method push_back() -> c:\program files\microsoft visual studio\vc98\include\xutility(39) : error C2679: binary '=' : no operator defined which takes a right-hand
10
4849
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
82
4030
by: Peter Olcott | last post by:
I need std::vector like capability for several custom classes. I already discussed this extensively in the thread named ArrayList without Boxing and Unboxing. The solution was to simply create non-generic (non C++ template) std::vector like capability for each of these custom classes. (Solution must work in Visual Studio 2002). Since I have already written one std::vector for a YeOlde C++ compiler (Borland C++ 1.0) that had neither...
13
2966
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
10
5032
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++ Primer - 4/e * * exercise 9.20 * STATEMENT: * Write a program to to compare whether a vector<intcontains the
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.