473,748 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1517
On Jul 16, 9:32*pm, "Alf P. Steinbach" <al...@start.no wrote:
* 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.programmin g].
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.programmin g, follow-ups set to comp.programmin g]
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**********@z7 2g2000hsb.googl egroups.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
3226
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 - a hybrid (XML atop relational database) Can anyone please provide me with links and preferably article on this topic (so that I can cite them in my paper)?
2
1555
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 specific C1 member function. Both containers contains the same number of elements (or at least, C1 is larger than C2). As suggested by S. Meyers (and others), I would like to use an STL algorithm instead of a custom loop. Let say I have the...
3
2633
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
1797
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 specifing: <pre> if len(x) 0: do_something() </pre>
1
1381
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 write without going out of bounds. Is something like this possible? I have something I whipped together that seems to work with the test cases I've given it, but I'm not 100% confident that it's bulletproof since it's been quite some time...
0
944
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 of issues do you run into. And also, what pieces of technology you have - which are open source and which ones you have are proprietary and any changes you anticipate 1 year out. Greatly appreciate your help. - John Smith
18
1818
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
2709
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 S-Video and Audio/Video connectors. Now, as far I understood during the tests I did yesterday, the VCR should be connected as 'Cable' - 'Set Top Box'. Is this correct? The second question: when connecting a Set Top Box, Media Center asks for
0
8991
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
8830
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9321
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
9247
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
6796
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
6074
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
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3312
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

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.