473,395 Members | 2,446 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,395 software developers and data experts.

Hybrid container

I am interested in having a container which has properties of both the
STL's list and vector. (I want my cake and to eat it too).

In my application, I will need to add/remove items from arbitrary
points in the container.

I will also need to be able to perform random access to elements of
the container -- accessed by index, not associatively.

Fortunately, in my application, I don't need to do both of these
things at the same time. I.e. I will do a bunch of adding/removing,
followed by a bunch of random access. Then I may go back to a bunch
of adding/removing, and then a whole bunch of random access.

So, my idea right now is to implement a container which is backed by a
list, but when you need to do random access, the list is 'frozen'. At
that point, a vector (or a boost array) is created to satisfy the
random access needs. Then, when you want to go back to inserting/
removing, the list is 'thawed' and the vector is destroyed.

I am still debating whether to make this freeze/thaw explicit or
automatic. Automatic is nice, but explicit would make sure the user
(me) doesn't screw up. Opinions appreciated.

Does this sort of thing exist already? Perhaps there is a keyword I'm
not searching for. Any pointers are appreciated.

I would rather not have to implement a complete STL container from
scratch, but from what I've read, they really aren't set up for
inheritance. What is the best way to go about implementing this idea?

Thanks in advance for any tips,

Rob
Jul 17 '08 #1
3 1497
On Jul 16, 9:32*pm, "Alf P. Steinbach" <al...@start.nowrote:
* Rob McDonald:
I am interested in having a container which has properties of both the
STL's list and vector. (I want my cake and to eat it too).
In my application, I will need to add/remove items from arbitrary
points in the container.
I will also need to be able to perform random access to elements of
the container -- accessed by index, not associatively.
Fortunately, in my application, I don't need to do both of these
things at the same time. *I.e. I will do a bunch of adding/removing,
followed by a bunch of random access. *Then I may go back to a bunch
of adding/removing, and then a whole bunch of random access.
So, my idea right now is to implement a container which is backed by a
list, but when you need to do random access, the list is 'frozen'. *At
that point, a vector (or a boost array) is created to satisfy the
random access needs. *Then, when you want to go back to inserting/
removing, the list is 'thawed' and the vector is destroyed.
I am still debating whether to make this freeze/thaw explicit or
automatic. *Automatic is nice, but explicit would make sure the user
(me) doesn't screw up. *Opinions appreciated.
Does this sort of thing exist already? *Perhaps there is a keyword I'm
not searching for. *Any pointers are appreciated.
I would rather not have to implement a complete STL container from
scratch, but from what I've read, they really aren't set up for
inheritance. *What is the best way to go about implementing this idea?

"Best" is meaningless without some criteria.

In addition to clear criteria for that, the specification needs to be fleshed
out. E.g., are the elements small ones (like char) or large ones? During adding,
do you need to insert elements at random positions, or are elements just added
at ends or perhaps one end, or, for example, is the insert position only moved
one element position at a time?
My intent would be to end up with a template container which would be
well behaved no matter the size of what I'm holding. In my immediate
application, it would probably hold a pointer to something more
complex.

Typically, when I will add to the list, I won't really care where they
go, so at the end is fine.

On the other hand, removals need to happen from arbitrary locations.
Depending on this there may already be a suitable C++ standard library
container, or in some other library, or you may have to create your own one. In
the latter case the question doesn't really have much to do with C++. So you'd
be better off asking in e.g. [comp.programming].
That is exactly the kind of guidance I was hoping for. Hopefully my
clarifications to your questions will help answer these questions.
[cross-posted to comp.programming, follow-ups set to comp.programming]
I appreciate the redirection, but I really think this question is more
appropriate to a C++/STL group than a general programming group. I am
developing in C++, using STL containers, with some Boost libraries,
and the any_iterator library to make things a bit more flexible. I
see my question as an implementation question rather than an algorithm/
design question.

[switched back to comp.lang.c++, not cross-posted]
Jul 17 '08 #2
On Jul 17, 12:18*am, Rob McDonald <rob.a.mcdon...@gmail.comwrote:
I am interested in having a container which has properties of both the
STL's list and vector. (I want my cake and to eat it too).

In my application, I will need to add/remove items from arbitrary
points in the container.

I will also need to be able to perform random access to elements of
the container -- accessed by index, not associatively.

Fortunately, in my application, I don't need to do both of these
things at the same time. *I.e. I will do a bunch of adding/removing,
followed by a bunch of random access. *Then I may go back to a bunch
of adding/removing, and then a whole bunch of random access.

So, my idea right now is to implement a container which is backed by a
list, but when you need to do random access, the list is 'frozen'. *At
that point, a vector (or a boost array) is created to satisfy the
random access needs. *Then, when you want to go back to inserting/
removing, the list is 'thawed' and the vector is destroyed.

I am still debating whether to make this freeze/thaw explicit or
automatic. *Automatic is nice, but explicit would make sure the user
(me) doesn't screw up. *Opinions appreciated.

Does this sort of thing exist already? *Perhaps there is a keyword I'm
not searching for. *Any pointers are appreciated.

I would rather not have to implement a complete STL container from
scratch, but from what I've read, they really aren't set up for
inheritance. *What is the best way to go about implementing this idea?

Thanks in advance for any tips,

Rob
As a first cut I would use a std::deque. It allows random access to
it's
elements (like a std::vector) but does not have the requirement that
it's
memory is contiguous (in that sense it's like a std::list).

HTH
Jul 17 '08 #3
In article <b14f7eb4-0434-43a5-b174-
7d**********@z72g2000hsb.googlegroups.com>, ro************@gmail.com
says...

[ ... ]
So, for the implementation, what I'd like is an STL vector with
remove(i), remove_if, remove(<Tt) etc. implemented as swap & remove.
Actually, if you look carefully, you'll find that all remove, remove_if,
etc., do is swap things to the end of the container. You have to use the
container's erase() to actually delete the items. Nicely enough, remove
and company return an iterator to the beginning of where the put the
items you asked to have removed, so you do something like:

myVector.erase(remove(whatever), myVector.end());

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 17 '08 #4

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

Similar topics

5
by: Markus Seibold | last post by:
Dear NG, I am working on a student project about a mobile tourism information system and among others I have to answer the question whether to use: - a relational database - a XML-native database...
2
by: Maitre Bart | last post by:
What I want to perform is calling a member function of container 1 (CRunner), using as argument the value of a container 2 (CNames). The purpose is to transfer values from C2 into C1 using a...
3
by: jignesh shah | last post by:
Hi all, Is there a way to recover a single container if its been corrupted or mark bad without restoring whole tablespace? environment: db28.1/aix5.1/tsm/rs-6000. Regards Jignesh
9
by: horizon5 | last post by:
Hi, my collegues and I recently held a coding style review. All of the code we produced is used in house on a commerical project. One of the minor issues I raised was the common idiom of...
1
by: Zachary Turner | last post by:
I want sort of a hybrid between these two. I want an iterator where * operator can both read from and write to elements in the collection, intelligently calling push_back if necessary in order to...
0
by: johnsmith3853 | last post by:
Hey Guys, I am still in a thinking stage and will like to learn from your experience, and was wondering if any of you folks have a hybrid environment i.e. Linux and Proprietary systems and what kind...
18
by: Goran | last post by:
Hi @ all! Again one small question due to my shakiness of what to use... What is better / smarter? private: vector<MyClass_t* itsVector; OR...
4
by: =?Utf-8?B?Q2xhdWRpbyBDYXNvbmF0bw==?= | last post by:
Hi. I have a PC running VISTA Home Premium 32 bits SP1 (AThlon 64 x2 5200, 2 giga RAM, Radeon 8600 GT 512). As TV tuner I have the PINNACLE PCTV Hybrid Tuner Kit, that include a connection for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
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.