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


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

C++ Multiset get_allocator() 函數用於返回分配器對象的副本,這有助於構造多集容器。

用法

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

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

參數

返回值

get_alloactor() 函數返回一個與多集容器關聯的分配器。

複雜度

恒定。

迭代器有效性

沒有變化。

數據競爭

容器被訪問。

同時訪問 multiset 的元素是安全的。

異常安全

這個函數從不拋出異常。

例子1

讓我們看一個簡單的例子:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   multiset<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 ()
{
  multiset<int> mymultiset;
  int * p;
  unsigned int i;

  // allocate an array of 5 elements using mymultiset's allocator:
  p=mymultiset.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';

  mymultiset.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;  
   
   multiset <int>::allocator_type s1_Alloc;  
   multiset <int>::allocator_type s2_Alloc;  
   multiset <double>::allocator_type s3_Alloc;  
   multiset <int>::allocator_type s4_Alloc;  
  
   // The following lines declare objects  
   // that use the default allocator.  
   multiset <int> s1;  
   multiset <int, allocator<int> > s2;  
   multiset <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 multiset s4  
   // with the allocator of multimultiset s1.  
   multiset <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 () 
{ 
  multiset < 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




相關用法


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