Next, we said that we can define our own comparator, and if we don’t define it, the default will be std::less and it will arrange in descending order that is, 8 will be given higher priority than 4, 5, 3, 6, 2. These must be there and these are supported by vector and deque. The queue also satisfies that requirement and another requirement was that it must provide the following functions: front() push_back(), and pop_back(). So, it has to satisfy this whatever container it is and in fact, vector satisfies that and it’s the default. And for example, a pointer to an element of the array satisfies the requirement of a legacy random access iterator. So, by sequence container, we mean a container that stores the object of the same type in a linear arrangement, and the iterators must satisfy the requirements of LegacyRandomAccessIterator which is a legacy bidirectional iterator that can be moved to point at any element in constant time. It is the type of underlying container to use to store the elements and the requirement is that whatever container you decide to use it has to meet the requirements of a sequence container. Now let us see the container Container in C++: You can see here, one of the elements is the container and the default is a vector. The above line will become mandatory when the element is of type some custom user-defined class and then you will need to define a custom comparator but even for primitive types you can define your own comparator. So larger values will get higher priority although we will see an example of this also where we will provide our own custom comparator and we will change the priority. So, it will arrange the larger values ahead of a smaller values. Then you can provide the compare and if you don’t provide anything the default will be std::less. It is an adapter so there is an underlying container and by default, the container will be a vector of the same type. So, you have a type of data ‘T’ that is stored in the container. This comparison will be used for determining the priority and this will be defined as, You need to include a ‘queue’ header file and a user-provided compare can be supplied to change the ordering. Insertions and extractions will take logarithmic time. Maybe in your use case, 4 has a higher priority so you can assign that. You can provide a custom compare function where you will define the order you want. Although you can define your own priority. So, by default 8 will have higher priority. For example, if we have integers 8 and 4 then we would say that 8 is more than 4. You will pick the element with the highest priority and by default, the highest priority will be given to the largest element.īy largest element means, we have a way of comparing the elements. It’s a queue where elements have different priorities. It provides a constant time lookup of the largest element (by default). Now, we will study another important container adapter that is called the priority queue. Please read our previous article, where we discussed Queue in C++ with Examples. In this article, I am going to discuss Priority Queue in C++ with Examples.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |