📜  C++ STL中的priority_queue value_type

📅  最后修改于: 2021-05-30 09:18:34             🧑  作者: Mango

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

句法:

priority_queue::value_type variable_name

它没有参数,也没有返回值。

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

程序1:

// C++ program to illustrate the
// priority_queue :: value_type function
#include 
using namespace std;
  
// Driver code
int main()
{
    // declare value_type for priority queue
    priority_queue::value_type AnInt;
  
    // Declares priority_queue
    priority_queue 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;
}
输出:
The value_type is AnInt = 20
The element at the top of the priority_queue is 30.

程式2:

#include 
using namespace std;
  
// Driver code
int main()
{
    // declare value_type for priority queue
    priority_queue::value_type AString;
  
    // Declares priority_queue
    priority_queue 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;
}
输出:
The value_type is AString = geeks for geeks
Value stored in priority queue are :
    ghi    def    abc
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”