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


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