priority_queue value_type in C++ STL
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
priority_queue< int >::value_type AnInt;
priority_queue< int > q1;
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;
int main()
{
priority_queue<string>::value_type AString;
priority_queue<string> q2;
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;
}
|
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
Share your thoughts in the comments
Please Login to comment...