473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

appending/joining STL::deque/list containers in constant time

This is probably somewhat of a beginners question but I am unfamiliar
with alot of aspects of C++. What is the correct way to append
multiple STL containers together in constant time. Obviously you can
do this with a linked list but is it possible to do the same thing
with the standard containers?

Thanks in advance,

Carter.
Jun 27 '08 #1
6 5264
On May 13, 10:34 am, Carter <carterch...@gmail.comwrote:
This is probably somewhat of a beginners question but I am unfamiliar
with alot of aspects of C++. What is the correct way to append
multiple STL containers together in constant time. Obviously you can
do this with a linked list but is it possible to do the same thing
with the standard containers?

Thanks in advance,

Carter.
If they both contain the same data type, then iterate through one
while pushing onto the other?
a linked list is a standard STL container btw. All STL containers
provide an iterator which allows access to the underlying data and all
STL container have some kind of insertion method. Depending on the
containers in question, some allow a range of iterators for insertion.
Jun 27 '08 #2
On May 14, 2:11 am, Christopher <cp...@austin.rr.comwrote:
On May 13, 10:34 am, Carter <carterch...@gmail.comwrote:
This is probably somewhat of a beginners question but I am unfamiliar
with alot of aspects of C++. What is the correct way to append
multiple STL containers together in constant time. Obviously you can
do this with a linked list but is it possible to do the same thing
with the standard containers?
Thanks in advance,
Carter.

If they both contain the same data type, then iterate through one
while pushing onto the other?
a linked list is a standard STL container btw. All STL containers
provide an iterator which allows access to the underlying data and all
STL container have some kind of insertion method. Depending on the
containers in question, some allow a range of iterators for insertion.
Well this works in time proportional to the lengths of the lists in
question. I was hoping for something which could like a hand coded
link list do a bit better. i.e. be proportional to the number of
merged lists. i.e. if for example I crafted a hand coded container
like this

template<typename T>
struct elem
{
T data;
elem* prev, next;
};

template<typename T>
struct
{
elem<T>* head, tail;
};

I could just set tail->next of list A to head of B and head->prev of
list B to tail of A. etc. which seems like a much faster operation
than manually inserting each element.

Regards,

Carter.
Jun 27 '08 #3
In article <4bf0797c-e72f-4607-b0c1-8eaba4afcd44
@z24g2000prf.googlegroups.com>, ca*********@gmail.com says...
This is probably somewhat of a beginners question but I am unfamiliar
with alot of aspects of C++. What is the correct way to append
multiple STL containers together in constant time. Obviously you can
do this with a linked list but is it possible to do the same thing
with the standard containers?
If you're using std::list, you can use list::splice to do the job. I
don't believe any of the other standard containers offers an equivalent.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #4
On May 13, 2:41 pm, Carter <carterch...@gmail.comwrote:
On May 14, 2:11 am, Christopher <cp...@austin.rr.comwrote:
On May 13, 10:34 am, Carter <carterch...@gmail.comwrote:
This is probably somewhat of a beginners question but I am unfamiliar
with alot of aspects of C++. What is the correct way to append
multiple STL containers together in constant time. Obviously you can
do this with a linked list but is it possible to do the same thing
with the standard containers?
Thanks in advance,
Carter.
If they both contain the same data type, then iterate through one
while pushing onto the other?
a linked list is a standard STL container btw. All STL containers
provide an iterator which allows access to the underlying data and all
STL container have some kind of insertion method. Depending on the
containers in question, some allow a range of iterators for insertion.

Well this works in time proportional to the lengths of the lists in
question. I was hoping for something which could like a hand coded
link list do a bit better. i.e. be proportional to the number of
merged lists. i.e. if for example I crafted a hand coded container
like this

template<typename T>
struct elem
{
T data;
elem* prev, next;

};

template<typename T>
struct
{
elem<T>* head, tail;

};

I could just set tail->next of list A to head of B and head->prev of
list B to tail of A. etc. which seems like a much faster operation
than manually inserting each element.

Regards,

Carter.
No you couldn't.
Because anyone performing a subsequent operation on A would then be
altering B and vica versa. you know longer have two independant
objects. It would indeed be very bad if you did not make a copy and
started having internals of an independent object pointing to memory
used by another. Shallow copy vs Deep copy, you want the latter.

Jun 27 '08 #5
On May 14, 5:37 am, Jerry Coffin <jcof...@taeus.comwrote:
In article <4bf0797c-e72f-4607-b0c1-8eaba4afcd44
@z24g2000prf.googlegroups.com>, carterch...@gmail.com says...
This is probably somewhat of a beginners question but I am unfamiliar
with alot of aspects of C++. What is the correct way to append
multiple STL containers together in constant time. Obviously you can
do this with a linked list but is it possible to do the same thing
with the standard containers?

If you're using std::list, you can use list::splice to do the job. I
don't believe any of the other standard containers offers an equivalent.

--
Later,
Jerry.

The universe is a figment of its own imagination.
I will give this a try thanks. It seems to be an interesting
limitation that
for the std::deque you cannot do the same thing. In my cast a
std::list should do
the trick though. I understand the objection raised too- but it seems
a bit
unfortunate that given we have library of linked and doubly linked
lists that
you cannot perform shallow merging in cases where the containers being
merged from
are being discarded.

Regards,

Carter.
Jun 27 '08 #6
In article <971cc250-794e-412a-b1fa-
9c**********@u36g2000prf.googlegroups.com>, ca*********@gmail.com
says...

[ ... ]
I understand the objection raised too- but it seems a bit
unfortunate that given we have library of linked and doubly linked
lists that you cannot perform shallow merging in cases where the
containers being merged from are being discarded.
That's exactly what list::splice does -- a destructive movement of
elements from one list to another in constant time.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #7

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

Similar topics

12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
3
by: zero | last post by:
First a short discription of my program. I have a bot that connects to an internet chess server. Whenever the bot logs on, it loads certain information from file, and puts it in STL containers...
6
by: JustSomeGuy | last post by:
I have an stl list that grows to be too huge to maintain effectivly in memory. There are elements within the list that could be stored on disk until accessed. However I don't want to expose...
2
by: Barry Hynes | last post by:
G'Day folks, Have been working on this problem for quite some time and still no farther ahead. :( Here is my problem...bare with me i am very green :) I have to implement a Safe List,...
5
by: Matthias Kaeppler | last post by:
Hi, I was wondering, since STL containers are based around copying, whether it's a good idea to use reference counted smart pointers, such as boost::shared_ptr in STL containers. I can't store...
6
by: Jonathan | last post by:
Hi. I'm having trouble figuring out what I should be doing here. I'm trying to remove an object from a list. The function is: void Alive::FromRoom () { list<Alive>::iterator iter =...
3
by: Christian Christmann | last post by:
Hi, reading the output of gprof for one of my projects, I found that the STL list assignment operator consumes a larger fraction of the program's execution time. The exact entry in gprof's...
9
by: toton | last post by:
Hi, I am looking for a circular buffer solution ( a fixed buffer) , where the elements can be pushed back & removed front. It looks stl deque can be a solution. my question is, 1) circular...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.