當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ priority_queue value_type用法及代碼示例

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 <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; 
}
輸出:
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 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



相關用法


注:本文由純淨天空篩選整理自Aman Goyal 2大神的英文原創作品 priority_queue value_type in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。