I will start a new series of introduction - C++ programming and their libraries! If anyone has ever heard about C++ programming, their may also know Standard Template Library, or more concisely, STL. There is no doubt that STL is extremely important in C++ programming, but be careful as many languages have their standard libraries, C++ also has its standard library, but STL and the standard library are different.
C++ STL is implemented based on C++ template, or metaprogramming. C++ template provides compile-time polymorphism - which could be more efficient regarding performance than traditional run-time polymorphism such as inheritance in C++. Below is an example to the C++ template based container,
Here we denote a vector, which is from C++ STL’s container. And we specify the vector v stores integers. Inside the brackets is a template argument, which should be a template parameter before instantiation. The type of v can be decided by C++ compiler during compile time, which is before runtime, thus making the polymorphism more efficient comparing with that if the type was defined at runtime as that way would definitely introduce footprint at runtime.
Besides the containers we just introduced, C++ STL provides four components including,
Where in my everyday programming, containers and functions from STL are quite often used. For containers, such as vector, stack are derived from Abstract Data Type(ADT). ADT does not have a universal standard because of some historical reasons, so when the programmers are dealing with STL containers, they are strongly advised to check the references.
The STL contains sequence containers, ordered associative containers and unordered associative containers. Sequence containers allow elements to be accessed sequentially, or Sequential accessible. Ordered associative containers implement sorted data structures that can be quickly searched, that means the elements in an ordered associative container are sorted(stronger ordered). Unordered associative containers are unordered, and also support quick search.
There are also container adaptors that adapt containers above for some purposes. For example, a stack or a queue can be adapted from a vector or a list. This would provide a simple and extensible way to have more data collections available in C++.
The C++ STL provides five types of iterators that work with the containers to operate on the elements. Some STL algorithms also accept iterators as parameters to help perform some operations on the containers or the elements.
Apart from containers, C++ STL also provides a set of algorithms. For example, C++ STL a function transform that applies a function to a range of elements, storing results in a destination range, which is the C++ version of a map skeleton that is dominant in many functional programming languages.
The C++ STL also includes classes that overload the function call operator (operator()). Instances of such classes are called functors or function objects, which can be arguments to the algorithms in the STL.
I am not going to cover more details about C++ STL, as there are tow many concepts and terminologies. I would like to introduce a specific STL implementation which I found very interesting. Before that, I would like to discuss some popular implementations that programmers use in their everyday coding. Let us merge C++ STL and C++ standard library here as the difference can be ignored temporally. Many frameworks provide STL implementations of which these are popular and commonly used.
- libc++ from Clang compiler
- libstdc++ from GCC
- libstdcxx from Apache
- DPC++ from oneAPI
- Microsoft STL from MSVC
- EASTL from the famous gaming company
- BerkeleyDB STL from Berkeley database project
The mostly used should be libstdc++, libc++ and Microsoft STL. BerkeleyDB seems to be a less popular STL implementation, however, I have found many interesting points inside it. As there are few posts about BerkeleyDB STL, I would like to learn and introduce it more in the near future.