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::priority_queue
that allows you to add members to the back of the member list efficiently. In this post, we explain what std::priority_queue
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 The Priority Queue (std::priority_queue) In Modern C++?
In programming, a Queue is an abstract data type in which members are stored in a container sequence and new members can be added to the end of the sequence and the first member can be extracted.
In C++, the priority_queue (std::priority_queue) is a container adapter defined in <queue> header that can be used to store members of a specific container in order, it uses logarithmic insertion and extraction methods to maintain its sorted structure.
Is there a simple example about the priority_queue (std::priority_queue) in modern C++?
Here is an example how you can define and add new members to the back of std::priority_queue
.
1 2 3 4 5 6 7 |
std::priority_queue< std::string > myq; myq.push("Maroon"); myq.push("Viloet"); myq.push("Aqua"); |
Here is another example how you can delete members from the top;
1 2 3 4 |
std::priority_queue<std::string> myq = { "Maroon", "Aqua", "Violet", "Ice Blue", "Crimson" }; myq.pop(); // Deletes Violet which is the top in order |
Is there a full example about the priority_queue (std:: priority_queue) in modern C++?
Here is a full example about std::queue
,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> #include <queue> #include <vector> #include <string> #include <functional> int main() { std::priority_queue< std::string > myq; // std::priority_queue< std::string, std::vector<std::string>, std::greater<std::string> > myq; myq.push("Maroon"); myq.push("Viloet"); myq.push("Aqua"); myq.push("Ice Blue"); myq.push("Chrimson"); std::cout << "Size: of my queue: " << myq.size() << std::endl; myq.pop(); // delete top member in order, "Violet" std::cout << "My queue members: "; for (; !myq.empty(); myq.pop()) std::cout << myq.top() << ", "; std::cout << std::endl; std::cout << "Size: of my queue: " << myq.size() << std::endl; system("pause"); return 0; } |
and the output of queue members will be ordered with Z … to … A as below,
1 2 3 4 5 |
Size: of my queue: 5 My queue members: Maroon, Ice Blue, Chrimson, Aqua, Size: of my queue: 0 |
As you see, “Violet” color which is the top member in order is extracted. If you want to sort A … to … Z, you should define myq
as below,
1 2 3 |
std::priority_queue< std::string, std::vector<std::string>, std::greater<std::string> > myq; |
There is also concurrent queue in modern C++. For more details about Concurrent Queue feature in C++, please see this paper; p0260r7
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition