The most powerful data storage arrays in Modern C++ are Containers and they are very useful to iterate and search members with their amazing methods and properties. A container is a holder object that stores data elements (a collection of data objects). std::vector
, std::array
, std::list
are these kinds of containers. There is another useful container std::deque
that allows you to add members to the front or the back of the member list efficiently. In this post, we explain what std::deque
is and give some examples. Before that let’s remind ourselves what containers are in C++ programming and what are their types.
Table of Contents
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 powerful 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.
What are the basic container types modern C++?
The C++ Standards library defines 4 container types.
- Sequence Containers ( vector, array, deque, list, forward_list )
- Associative Containers ( map, multimap, set, multiset )
- Unordered Associative Containers ( unordered_map, unordered_multimap, unordered_set, unordered_multiset )
- Container Adapters ( stack, queue, priority_queue )
If you want to learn more about these each type, here are some basic details,
What Is deque (std::deque) In Modern C++?
The deque container (std::deque
) (acronym of the double-ended queue) is an indexed Sequence Container defined in <deque>
header that is faster in insertion and deletion on both beginning (by using push_front
) and end (by using push_back
) sides and its size automatically changes by insertion and deletion methods. The deque does not invalidate pointers or references of its members when you use these insertion methods or deletion methods, while the std::vector
does. Its usage is a kind of similar to <vector> containers, members of deque are not stored contiguously, and there is a push_front()
method.
Here is a very general declaration in modern C++:
and since C++17 it is defined under pmr namespace as shown below.
The std::deque
is a very fast sequence container when adding or deleting new members but note that it costs much more memory than a std::vector
too. We can use push_back
, and emplace_back
methods to add elements to the end, we can use push_front
, and emplace_front
methods to add elements to the front.
We can use begin()
, end()
, … iterations, and ranges to operate and list them. We can use the insert
or insert_range()
method to add pair values. There are other useful modifier methods, such as; clear()
, emplace()
, erase()
, swap()
, extract()
, merge()
operations.
There is no find method to search by a key value as in std::list sequence containers.
Let’s see how we can use it with some simple examples.
Is there a simple example about deque (std::deque) in modern C++?
Here is an example how you can define and add new members to the front and back of std::deque
.
Here is another example how you can delete members from front and back;
Is there a full example about deque (std::deque) in modern C++?
Here is a full example about std::deque
For more details about this feature in C++11 standard, please see this paper; p1923r0
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 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 here.