C++C++11C++14C++17C++20Learn C++

What Are The Amazing Containers In Modern C++?

What Are The Amazing Containers In Modern C++ These are not the containers you are looking for

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;

  • array (std::array) : a class template for the static contiguous array (modern C array)
  • vector (std::vector) : a class template for the dynamic contiguous array (modern dynamic C arrays)
  • deque (std::deque) : a class template for the double-ended queue
  • list (std::list) : a class template for the doubly-linked list (modern linked list)
  • forward_list (std::forward_list) : a class template for the singly-linked list (modern linked list)

What are associative 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;

  • map (std::map) : a class template for the collection of key-value pairs, its keys are unique and it is sorted by keys
  • set (std::set) : a class template for the collection of unique keys, it is sorted by keys 
  • multiset (std::multiset) : a class template for the collection of keys, it is sorted by keys
  • multimap (std::multimap) : a class template for the collection of key-value pairs, it is sorted by keys 

What are unordered 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 (std::unordered_set) : a class template for the collection of unique keys, it is hashed by keys
  • unordered_map (std::unordered_map) : a class template for the collection of key-value pairs, its keys are unique and hashed by keys
  • unordered_multiset (std::unordered_multiset) : a class template for the collection of keys, it is hashed by keys
  • unordered_multimap (std::unordered_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
What Are The Amazing Containers In Modern C++

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 

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?