priority_queue value_type in C++ STL

The priority_queue :: value_type method is a builtin function in C++ STL which represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter.

Syntax:

priority_queue::value_type variable_name

It has no parameters and no return value.

Below programs illustrate the above function.

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// priority_queue :: value_type function
#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main()
{
    // declare value_type for priority queue
    priority_queue<int>::value_type AnInt;
  
    // Declares priority_queue
    priority_queue<int> q1;
  
    // here AnInt acts as a varibale of int data type
    AnInt = 20;
    cout << "The value_type is AnInt = " << AnInt << endl;
  
    q1.push(AnInt);
    AnInt = 30;
    q1.push(AnInt);
  
    cout << "The element at the top of the priority_queue is "
         << q1.top() << "." << endl;
  
    return 0;
}

chevron_right


Output:

The value_type is AnInt = 20
The element at the top of the priority_queue is 30.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
// Driver code
int main()
{
    // declare value_type for priority queue
    priority_queue<string>::value_type AString;
  
    // Declares priority_queue
    priority_queue<string> q2;
  
    // here AnInt acts as a varibale of int data type
    AString = "geeks for geeks";
    cout << "The value_type is AString = " << AString << endl;
  
    AString = "abc";
    q2.push(AString);
    AString = "def";
    q2.push(AString);
    AString = "ghi";
    q2.push(AString);
  
    cout << "Value stored in priority queue are :" << endl;
    while (!q2.empty()) {
        cout << '\t' << q2.top();
        q2.pop();
    }
  
    return 0;
}

chevron_right


Output:

The value_type is AString = geeks for geeks
Value stored in priority queue are :
    ghi    def    abc


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.