Click here to Skip to main content
13,247,608 members (46,241 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 6 Mar 2001

STL containers map, set, and list with fast array access

, 5 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Source code for STL compliant container classes that add fast indexing capability to existing container types
<!-- Link to source file download --> <!-- Add the rest of your HTML here -->


This source code contains five STL compliant container classes with the following names:


Each of these implements the same interface as the STL class in the second part of its name. In addition, each one also implements the STL interface for the vector<T> class, providing fast array access via operator[] and at(). T is the type of the contained class. Key is the type of the lookup key for pair-associative containers. Cmp is the comparison predicate and must be a binary predicate with arguments of type T for set and type Key for map. If you do not provide a comparison predicate, it will default to std::less<Arg>.

The classes are all derived from an AVL tree that I borrowed form Adreas Jager (copyright included) and modified to allow for the indexed access. Each tree node contains an extra long integer that holds the number of child nodes below it. The algorithm to update these counts does not add to the complexity of any of the original tree algorithms. Since the interfaces are documented in the STL, I will simply provide the complexity analysis below. I believe I have covered the most-used functions of these containers, but some functions may not be included. Please contact me if you wish to add another STL function and I will be glad to update the source.

Complexity Analysis


An index_list is a sequence which combines the interfaces of array, deque, and list.

Here are the complexity comparisons with other sequence containers:

array deque list index_list
Indexed access 1 1 (N) lgN
Insert/delete N N 1 1+
Front insertion(N) 1+ 1 1+
Back insertion 1+ 1+ 1 1+

It tends to work best for large sets (N >> 1).

It also has the extra function insert(const T&), usually used for sets, which chooses an insert position that keeps the tree balanced with a minimal number of rotation operations.


These classes implement the STL interfaces for set, multiset, map, and multimap respectively, along with the indexed access (operator[], at()). Here are the complexity comparisons with other associative containers:

arraylistsetindex_set/map etc.
Indexed access 1 (N) (N+) lgN
Insert/delete N 1 1+ 1+
Search lgNN lgN lgN



21 July 2002 - updated downloads

6 Aug 2002 - updated downloads


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ray Virzi
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generalsome small extensions Pin
George Anescu4-Oct-02 4:51
memberGeorge Anescu4-Oct-02 4:51 
GeneralRe: some small extensions Pin
Ray Virzi4-Oct-02 21:06
memberRay Virzi4-Oct-02 21:06 
Interesting extensions to the standard library. I must point out that the GetIndex() function does not work for the _endnode marker, so you must check for that condition. What is 'index' set to when the item is not found or cannot be inserted? Perhaps -1?

I think the conventional way to get the index is to take the iterator returned by both find() and insert() and subtract it from the beginning of the container, like so:

index_set<T> myset;
index_set<T>::iterator at;
unsigned index;

at = myset.insert(T()).first; (or at = myset.find(T());)
index = myset.begin() - at;

This should give you the 0-based index. If the function fails, at is set to myset.end() and index will be the total size of the container, so this condition must still be checked for.

But I have also noticed a bug in my code as well. The insert function won't work this way for set and map if the item cannot be inserted. This is because the end maker must point to _endnode. My last line of the insert function should read:

return std::make_pair(AVLTree_Iterator(*this, (pos ? pos : _endnode)), found);

I will upload a corrected version of the code when I get a chance.

GeneralRe: some small extensions Pin
George Anescu7-Oct-02 6:55
memberGeorge Anescu7-Oct-02 6:55 
GeneralRe: some small extensions Pin
Ray Virzi7-Oct-02 17:35
memberRay Virzi7-Oct-02 17:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 6 Aug 2002
Article Copyright 2001 by Ray Virzi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid