Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a readable implementation of the STL? [closed]

Tags:

c++

stl

People also ask

How are STL containers implemented?

The standard doesn't specify what data structures should be used to implement STL containers but it does make complexity guarantees and sometimes other guarantees (like a vector using contiguous memory) that generally restrict the choice of data structures that could reasonably be used by a conforming implementation to ...

How are STL containers implemented in C++?

A container is a holder object that stores a collection of other objects (its elements). They are implemented as class templates, which allows great flexibility in the types supported as elements.

Who implements C++ Standard Library?

The two most popular open source implementations of standard C++ library are: libstdc++, part of gcc project. libc++, part of LLVM project.

Is the standard library open source?

Today at CppCon 2019, we (the MSVC team) announced that we're releasing our implementation of the C++ Standard Library (also known as the STL) as open source. https://github.com/microsoft/STL is our new repository, containing all of our product source code, a new CMake build system, and a README with more information.


There is a book The C++ Standard Template Library, co-authored by the original STL designers Stepanov & Lee (together with P.J. Plauger and David Musser), which describes a possible implementation, complete with code - see http://www.amazon.co.uk/C-Standard-Template-Library/dp/0134376331.


Yes, there is original implementation of the STL by Alexander Stepanov and Meng Lee. It is the most readable STL implementation I have ever seen. You can download it from here.

Below is an implementation of pair. Note how readable the source code really was:

#include <bool.h>

template <class T1, class T2>
struct pair {
    T1 first;
    T2 second;
    pair() {}
    pair(const T1& a, const T2& b) : first(a), second(b) {}
};

template <class T1, class T2>
inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) { 
    return x.first == y.first && x.second == y.second; 
}

template <class T1, class T2>
inline bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y) { 
    return x.first < y.first || (!(y.first < x.first) && x.second < y.second); 
}

template <class T1, class T2>
inline pair<T1, T2> make_pair(const T1& x, const T2& y) {
    return pair<T1, T2>(x, y);
}

Back to the roots!


I use the The C++ Standard Library: A Tutorial and Reference and can highly recommend it. Of course it is not something you read cover-to-cover, but is an very handy reference. Check out the reviews on Amazon as well.


Note that GCC's STL headers have the tab stop set to eight. Reconfigure your editor or replace tabs with eight spaces and it should be much more readable.