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


C++ unordered_set get_allocator()用法及代碼示例

unordered_set的get_allocator()方法是C++標準模板庫(STL)的一部分。此方法獲取存儲的分配器對象並返回它。

用法:

Allocator_type get_allocator() const;

其中allocator_type是容器使用的分配器的類型。


返回值:它返回用於構造容器的分配器對象。

異常:在此方法中,如果任何元素比較對象引發異常,則引發Exception。

下麵的程序演示了unordered_set::get_allocator()函數

程序1:

// CPP program to illustrate 
// unordered_set get_allocator() 
  
#include <iostream> 
#include <unordered_set> 
  
using namespace std; 
int main() 
{ 
  
    //'c' is object of 'unordered_set' 
    unordered_set<int> c; 
  
    //'allocator_type' is inherit in 'unordered_set' 
    //'a' is object of 'allocator_type' 
    //'get_allocator()' is member of 'unordered_set' 
    unordered_set<int>::allocator_type a = c.get_allocator(); 
  
    // Comparing the Allocator with Pair<int, int> 
    cout << "Is allocator Pair<int, int>:"
         << boolalpha 
         << (a == allocator<pair<int, int> >()); 
  
    return 0; 
}
輸出:
Is allocator Pair:true

複雜:
執行操作需要花費恒定(O(1))的時間複雜度。

程序2:

// CPP program to illustrate 
// unordered_set get_allocator() 
  
#include <iostream> 
#include <unordered_map> 
  
using namespace std; 
  
int main(void) 
{ 
    unordered_map<char, int> um; 
    pair<const char, int>* a; 
  
    a = um.get_allocator().allocate(8); 
  
    cout << "Allocated size = " << sizeof(*a) * 8 << endl; 
  
    return 0; 
}
輸出:
Allocated size = 64


相關用法


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