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


C++ set get_allocator用法及代碼示例


描述

它返回與集合關聯的分配器對象的副本。

聲明

以下是 std::set::get_allocator 在各種 C++ 版本中的工作方式。

C++98

allocator_type get_allocator() const;

C++11

allocator_type get_allocator() const noexcept;

返回值

它返回與集合關聯的分配器對象的副本。

異常

如果拋出異常,則容器中沒有變化。

時間複雜度

時間複雜度取決於對數。

示例

下麵的例子展示了 std::set::get_allocator 的用法。

#include <iostream>
#include <set>

int main () {
   std::set<int> myset;
   int * p;
   unsigned int i;

   p = myset.get_allocator().allocate(5);
 
   for (i = 0; i < 5; i++) p[i]=(i+1)*10;

   std::cout << "The allocated array contains:";
   for (i = 0; i < 5; i++) std::cout << ' ' << p[i];
   std::cout << '\n';

   myset.get_allocator().deallocate(p,5);

   return 0;
}

上述程序將正確編譯和執行。

The allocated array contains:10 20 30 40 50

相關用法


注:本文由純淨天空篩選整理自 C++ Set Library - get_allocator Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。