What Are The Amazing Containers In Modern C++?
Containers are powerful data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties. The C++ Standards library defines 4 container types. In this post, we explain containers in modern C++.
What is a container in modern C++?
Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their amazing methods and properties.
A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, they are safe and optimized well.
Basically, a container manages the storage space for its elements and provides properties and methods to access and operate on them, these methods and properties can be either directly or through iterators. They are mostly dynamic data structures, and they are well optimized for the memory management and performance.
In C++, there are four main types of containers,
- Sequence Containers (vectors, arrays, …)
- Associative Containers (maps, sets, …)
- Unordered Associative Containers (unordered_set, unordered_map, …)
- Container Adapters (stack, queue, priority_queue)
Now, let’s see each of them.
What are sequence containers in modern C++?
In C++, the Sequence Containers are class templates of container types of modern C++ that can be used to implement data structure types (vector, array,…) where they can be accessed sequentially. They are a kind of data types, but objects of classes and they can use methods of its classes, optimized for many modern C++ algorithms and methods.
The sequence containers are;
std::array
: a class template for the static contiguous array (modern C array)std::vector
: a class template for the dynamic contiguous array ( modern dynamic C arrays)std::deque
: a class template for the double-ended queuestd::list
: a class template for the doubly-linked list (modern linked list)std::forward_list
: a class template for the singly-linked list (modern linked list)
What are sequence containers in modern C++?
Associative Containers are class templates of container types that can be used to implement sorted data structures where can be quickly searched. They are sorted by keys. We can say they are about O(log n) complexity data structures.
The associative containers are;
std::map
: a class template for the collection of key-value pairs, its keys are unique and it is sorted by keysstd::set
: a class template for the collection of unique keys, it is sorted by keysmultiset
: a class template for the collection of keys, it is sorted by keysmultimap
: a class template for the collection of key-value pairs, it is sorted by keys
What are associative containers in modern C++?
Unordered Associative Containers are class templates of container types that can be used to implement unsorted (hashed) data structures where they can be quickly searched. They are about O(1) amortized, O(n) worst-case complexity data structures.
The unsorted associative containers are;
unordered_set
: a class template for the collection of unique keys, it is hashed by keysunordered_map
: a class template for the collection of key-value pairs, its keys are unique and hashed by keysunordered_multiset
: a class template for the collection of keys, it is hashed by keys- u
nordered_multimap
: a class template for the collection of key-value pairs, i s hashed by keys
What are container adapters in modern C++?
Container Adapters are class templates of container types in modern C++, used to provide a different interface for sequential containers.
Container adapters are;
- stack : a class template that adapts a container to provide stack, LIFO data structure
- queue: a class template that adapts a container to provide queue, FIFO data structure
- priority_queue: a class template that adapts a container to provide priority queue
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from