当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。