TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ multiset get_allocator() function

C++ multiset get_allocator() Function with Examples on tutorial for beginners and professionals with examples on multiset, begin(), end(), multiset constructor, ~multiset destructor, operator=, rbegin(), rend() etc.

<< Back to CPP

C++ Multiset get_allocator()

C++ Multiset get_allocator() function is used to return the copy of allocator object which helps to construct the multiset container.

Syntax

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

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

Parameter

None

Return value

get_alloactor() function returns an allocator associated with multiset container.

Complexity

Constant.

Iterator validity

No changes.

Data Races

The container is accessed.

Concurrently accessing the elements of multiset is safe.

Exception Safety

This function never throws exceptions.

Example 1

Let's see the simple example:

#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;
}

Output:

Allocated size = 32

Example 2

Let's see a simple example:

#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;
}

Output:

The allocated array contains: 10 20 30 40 50

Example 3

Let's see a simple example to check whether the allocators are interchangeable or not:

#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;
}

Output:

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.

Example 4

Let's see a simple example:

#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 ); 
}

Output:

42, 43
Next TopicC++ multiset




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf