Open In App

priority_queue value_type in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

The priority_queue :: value_type method is a built-in 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. 

Time complexity: O(1)
Syntax: 

priority_queue::value_type variable_name

It has no parameters and no return value. 
Below programs illustrate the above function.
Program 1: 

CPP




// 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 variable 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;
}


Output

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

Program 2: 

CPP




#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 AString acts as a variable of string 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;
}
 
// This code is modified by Susobhan Akhuli


Output

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


Last Updated : 23 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads