C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Set get_allocator()

C++ Set get_allocator()

C++ Set get_allocator()函数用于返回分配器对象的副本,该对象有助于构造set容器。

语法

           allocator_type get_allocator() const;    //until C++ 11
allocator_type get_allocator() const noexcept;  //since C++ 11

参数

返回值

返回与集合容器关联的分配器。

复杂度

常量。

迭代器有效性

没有变化。

数据竞争

访问容器。
同时访问set元素是安全的。

异常安全性

此函数从不抛出异常。

示例1

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   set<double> m;   
   double *p;
   p = m.get_allocator().allocate(3);
   //size of double is 8
   cout << "Allocated size = " <<  sizeof(*p) * 4 << endl;
   return 0;
}
输出:
Allocated size = 32

示例2

让我们看一个简单的示例:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<int> myset;
  int * p;
  unsigned int i;
  // allocate an array of 5 elements using myset's allocator:
  p=myset.get_allocator().allocate(5);
  // assign some values to array
  for (i=0; i<5; i++) p[i]=(i+1)*10;
  cout << "The allocated array contains:";
  for (i=0; i<5; i++) cout << ' ' << p[i];
  cout << '\n';
  myset.get_allocator().deallocate(p,5);
  return 0;
}
输出:
The allocated array contains: 10 20 30 40 50

示例3

让我们看一个简单的示例来检查分配器是否可互换:
#include <set>  
#include <iostream>  
  
int main( )  
{  
   using namespace std;  
   set <int>::allocator_type s1_Alloc;  
   set <int>::allocator_type s2_Alloc;  
   set <double>::allocator_type s3_Alloc;  
   set <int>::allocator_type s4_Alloc;  
  
   // The following lines declare objects  
   // that use the default allocator.  
   set <int> s1;  
   set <int, allocator<int> > s2;  
   set <double, allocator<double> > s3;  
  
   s1_Alloc = s1.get_allocator( );  
   s2_Alloc = s2.get_allocator( );  
   s3_Alloc = s3.get_allocator( );  
  
   cout << "The number of integers that can be allocated"  
        << endl << "before free memory is exhausted: "  
        << s2.max_size( ) << "." << endl;  
  
   cout << "\nThe number of doubles that can be allocated"  
        << endl << "before free memory is exhausted: "  
        << s3.max_size( ) <<  "." << endl;  
  
   // The following line creates a set s4  
   // with the allocator of multiset s1.  
   set <int> s4( less<int>( ), s1_Alloc );  
  
   s4_Alloc = s4.get_allocator( );  
  
   // Two allocators are interchangeable if  
   // storage allocated from each can be  
   // deallocated by the other  
   if( s1_Alloc == s4_Alloc )  
   {  
      cout << "\nThe allocators are interchangeable."  
           << endl;  
   }  
   else  
   {  
      cout << "\nThe allocators are not interchangeable."  
           << endl;  
   }
   
   return 0;
}
输出:
The number of integers that can be allocated
before free memory is exhausted: 461168601842738790.
The number of doubles that can be allocated
before free memory is exhausted: 461168601842738790.
The allocators are interchangeable.

示例4

让我们看一个简单的示例:
#include <iostream>
 #include <set>
using namespace std;
int  main () 
{ 
  set < int >  c ; 
  int *  p ;
  p  =  c . get_allocator () . allocate ( 2 );
  p [ 0 ]  =  42 ; 
  p [ 1 ]  =  43 ;
  cout  <<  p [ 0 ]  <<  ", "  <<  p [ 1 ]  <<  endl ;
  c . get_allocator () . deallocate ( p ,  2 ); 
}
输出:
42, 43

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4