C++ STL中的priority_queue value_type

C++ STL中的priority_queue value_type

在C++ STL中, priority_queue :: value_type 方法是一个内置函数,它表示作为priority_queue中元素存储的对象类型。它充当template参数的同义词。

时间复杂度: O(1)

语法:

priority_queue::value_type variable_name

它没有参数和返回值。

下面的程序说明了上述函数。

程序1:

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

输出

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

程序2:

#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```  

输出

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

C++ 教程