Bagikan melalui


Kelas alokator

Templat kelas menjelaskan objek yang mengelola alokasi penyimpanan dan membebaskan untuk array objek jenis Type. Objek kelas allocator adalah objek alokator default yang ditentukan dalam konstruktor untuk beberapa templat kelas kontainer di Pustaka Standar C++.

Sintaks

template <class Type>
class allocator

Parameter

Jenis
Jenis objek tempat penyimpanan dialokasikan atau dibatalkan alokasinya.

Keterangan

Semua kontainer Pustaka Standar C++ memiliki parameter templat yang default ke allocator. Membangun kontainer dengan alokator kustom memberikan kontrol atas alokasi dan membebaskan elemen kontainer tersebut.

Misalnya, objek alokator mungkin mengalokasikan penyimpanan pada timbunan privat atau dalam memori bersama, atau mungkin mengoptimalkan untuk ukuran objek kecil atau besar. Ini mungkin juga menentukan, melalui definisi jenis yang disediakannya, elemen tersebut diakses melalui objek pengakses khusus yang mengelola memori bersama, atau melakukan pengumpulan sampah otomatis. Oleh karena itu, kelas yang mengalokasikan penyimpanan menggunakan objek alokator harus menggunakan jenis ini untuk mendeklarasikan objek pointer dan referensi, seperti yang dilakukan kontainer di Pustaka Standar C++.

(hanya C++98/03) Ketika Anda berasal dari kelas alokator, Anda harus menyediakan struct rebind , yang typedef-nya _Other mereferensikan kelas Anda yang baru diturunkan.

Dengan demikian, alokator mendefinisikan jenis berikut:

  • pointer berulah seperti penunjuk ke Type.

  • const_pointer berulah seperti penunjuk const ke Type.

  • referensi berulah seperti referensi ke Type.

  • const_reference berulah seperti referensi const ke Type.

Ini Typemenentukan formulir yang harus diambil pointer dan referensi untuk elemen yang dialokasikan. ( alokator::p ointer belum tentu sama dengan Type* untuk semua objek alokator, meskipun memiliki definisi yang jelas ini untuk kelas allocator.)

C++11 dan yang lebih baru: Untuk mengaktifkan operasi pemindahan di alokator Anda, gunakan antarmuka alokator minimal dan terapkan konstruktor salinan, == dan != operator, alokasikan dan batalkan alokasi. Untuk informasi selengkapnya dan contohnya, lihat Alokator

Anggota

Konstruktor

Nama Deskripsi
Pengalokasi Konstruktor yang digunakan untuk membuat allocator objek.

Typedefs

Nama Deskripsi
const_pointer Jenis yang menyediakan pointer konstan ke jenis objek yang dikelola oleh alokator.
const_reference Jenis yang menyediakan referensi konstan ke jenis objek yang dikelola oleh alokator.
difference_type Jenis integral yang ditandatangani yang dapat mewakili perbedaan antara nilai pointer dengan jenis objek yang dikelola oleh alokator.
Pointer Jenis yang menyediakan penunjuk ke jenis objek yang dikelola oleh alokator.
referensi Jenis yang menyediakan referensi ke jenis objek yang dikelola oleh alokator.
size_type Jenis integral yang tidak ditandatangani yang dapat mewakili panjang urutan apa pun yang dapat dialokasikan objek jenis allocator .
value_type Jenis yang dikelola oleh alokator.

Fungsi

Nama Deskripsi
alamat Menemukan alamat objek yang nilainya ditentukan.
Mengalokasikan Mengalokasikan blok memori yang cukup besar untuk menyimpan setidaknya beberapa elemen yang ditentukan.
Membangun Membuat jenis objek tertentu pada alamat tertentu yang diinisialisasi dengan nilai tertentu.
batalkan alokasi Membebaskan jumlah objek tertentu dari penyimpanan yang dimulai pada posisi tertentu.
Menghancurkan Memanggil destruktor objek tanpa membatalkan alokasi memori tempat objek disimpan.
max_size Mengembalikan jumlah elemen jenis Type yang dapat dialokasikan oleh objek kelas allocator sebelum memori bebas habis.
pengikatan ulang Struktur yang memungkinkan alokator untuk objek dari satu jenis untuk mengalokasikan penyimpanan untuk objek jenis lain.

Operators

Nama Deskripsi
operator= Menetapkan satu allocator objek ke objek lain allocator .

address

Menemukan alamat objek yang nilainya ditentukan.

pointer address(reference val) const;
const_pointer address(const_reference val) const;

Parameter

Val
Nilai const atau nonconst objek yang alamatnya sedang dicari.

Tampilkan Nilai

Pointer const atau nonconst ke objek yang ditemukan, masing-masing, nilai const atau nonconst.

Keterangan

Fungsi anggota mengembalikan alamat val, dalam bentuk yang harus diambil pointer untuk elemen yang dialokasikan.

Contoh

// allocator_address.cpp
// compile with: /EHsc
#include <memory>
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 1 ; i <= 7 ; i++ )
   {
      v1.push_back( 2 * i );
   }

   cout << "The original vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   allocator<int>::const_pointer v1Ptr;
   const int k = 8;
   v1Ptr = v1Alloc.address( *find(v1.begin( ), v1.end( ), k) );
   // v1Ptr = v1Alloc.address( k );
   cout << "The integer addressed by v1Ptr has a value of: "
        << "*v1Ptr = " << *v1Ptr << "." << endl;
}
The original vector v1 is:
( 2 4 6 8 10 12 14 ).
The integer addressed by v1Ptr has a value of: *v1Ptr = 8.

mengalokasikan

Mengalokasikan blok memori yang cukup besar untuk menyimpan setidaknya beberapa elemen yang ditentukan.

pointer allocate(size_type count, const void* _Hint);

Parameter

count
Jumlah elemen yang penyimpanannya memadai akan dialokasikan.

_Petunjuk
Pointer const yang dapat membantu objek alokator memenuhi permintaan penyimpanan dengan menemukan alamat objek yang dialokasikan sebelum permintaan.

Tampilkan Nilai

Penunjuk ke objek yang dialokasikan atau null jika memori tidak dialokasikan.

Keterangan

Fungsi anggota mengalokasikan penyimpanan untuk array elemen jumlah jenis Type, dengan memanggil operator baru(hitungan). Ini mengembalikan pointer ke objek yang dialokasikan. Argumen petunjuk membantu beberapa alokator dalam meningkatkan lokalitas referensi; pilihan yang valid adalah alamat objek yang dialokasikan sebelumnya oleh objek alokator yang sama dan belum dibatalkan alokasinya. Untuk tidak memberikan petunjuk, gunakan argumen pointer null sebagai gantinya.

Contoh

// allocator_allocate.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   allocator<int> v1Alloc;
   allocator<int>::pointer v1aPtr;
   v1aPtr = v1Alloc.allocate ( 10 );

   int i;
   for ( i = 0 ; i < 10 ; i++ )
   {
      v1aPtr[ i ] = i;
   }

   for ( i = 0 ; i < 10 ; i++ )
   {
      cout << v1aPtr[ i ] << " ";
   }
   cout << endl;
   v1Alloc.deallocate( v1aPtr, 10 );
}
0 1 2 3 4 5 6 7 8 9

allocator

Konstruktor yang digunakan untuk membuat objek alokator.

allocator();
allocator(const allocator<Type>& right);
template <class Other>
    allocator(const allocator<Other>& right);

Parameter

right
Objek alokator yang akan disalin.

Keterangan

Konstruktor tidak melakukan apa-apa. Namun, secara umum, objek alokator yang dibangun dari objek alokator lain harus dibandingkan dengan objek tersebut dan mengizinkan intermix alokasi objek dan membebaskan antara dua objek alokator.

Contoh

// allocator_allocator.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

class Int {
public:
   Int( int i )
   {
      cout << "Constructing " << ( void* )this  << endl;
      x = i;
      bIsConstructed = true;
   };
   ~Int( )
   {
      cout << "Destructing " << ( void* )this << endl;
      bIsConstructed = false;
   };
   Int &operator++( )
   {
      x++;
      return *this;
   };
   int x;
private:
   bool bIsConstructed;
};

int main( )
{
   allocator<double> Alloc;
   vector <Int>:: allocator_type v1Alloc;

   allocator<double> cAlloc(Alloc);
   allocator<Int> cv1Alloc(v1Alloc);

   if ( cv1Alloc == v1Alloc )
      cout << "The allocator objects cv1Alloc & v1Alloc are equal."
           << endl;
   else
      cout << "The allocator objects cv1Alloc & v1Alloc are not equal."
           << endl;

   if ( cAlloc == Alloc )
      cout << "The allocator objects cAlloc & Alloc are equal."
           << endl;
   else
      cout << "The allocator objects cAlloc & Alloc are not equal."
           << endl;
}
The allocator objects cv1Alloc & v1Alloc are equal.
The allocator objects cAlloc & Alloc are equal.

const_pointer

Jenis yang menyediakan pointer konstan ke jenis objek yang dikelola oleh alokator.

typedef const value_type *const_pointer;

Keterangan

Jenis penunjuk menjelaskan objek ptr yang dapat menunjuk, melalui ekspresi *ptr, objek const apa pun yang dapat dialokasikan oleh objek jenis allocator .

Contoh

// allocator_const_ptr.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 1 ; i <= 7 ; i++ )
   {
      v1.push_back( i * 2 );
   }

   cout << "The original vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   allocator<int>::const_pointer v1Ptr;
   const int k = 10;
   v1Ptr = v1Alloc.address( k );

   cout << "The integer's address found has a value of: "
        << *v1Ptr << "." << endl;
}
The original vector v1 is:
( 2 4 6 8 10 12 14 ).
The integer's address found has a value of: 10.

const_reference

Jenis yang menyediakan referensi konstan ke jenis objek yang dikelola oleh alokator.

typedef const value_type& const_reference;

Keterangan

Jenis referensi menjelaskan objek yang dapat menunjuk objek const apa pun yang dapat dialokasikan oleh objek jenis allocator .

Contoh

// allocator_const_ref.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <double> v;
   vector <double> ::iterator vIter, vfIter;
   vector <double> :: allocator_type vAlloc;

   int j;
   for ( j = 1 ; j <= 7 ; j++ )
   {
      v.push_back( 100.0 * j );
   }

   cout << "The original vector v is:\n ( " ;
   for ( vIter = v.begin( ) ; vIter != v.end( ) ; vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;

   vfIter = v.begin( );
   allocator<double>::const_reference vcref =*vfIter;
   cout << "The value of the element referred to by vref is: "
        << vcref << ",\n the first element in the vector." << endl;

   // const references can have their elements modified,
   // so the following would generate an error:
   // vcref = 150;
   // but the value of the first element could be modified through
   // its nonconst iterator and the const reference would remain valid
*vfIter = 175;
   cout << "The value of the element referred to by vcref,"
        <<"\n after nofication through its nonconst iterator, is: "
        << vcref << "." << endl;
}
The original vector v is:
( 100 200 300 400 500 600 700 ).
The value of the element referred to by vref is: 100,
the first element in the vector.
The value of the element referred to by vcref,
after nofication through its nonconst iterator, is: 175.

Membangun

Membuat jenis objek tertentu pada alamat tertentu yang diinisialisasi dengan nilai tertentu.

void construct(pointer ptr, const Type& val);
void construct(pointer ptr, Type&& val);
template <class _Other>
    void construct(pointer ptr, _Other&&... val);

Parameter

Ptr
Penunjuk ke lokasi tempat objek akan dibangun.

Val
Nilai tempat objek yang sedang dibangun akan diinisialisasi.

Keterangan

Fungsi anggota pertama setara dengan new ((void *) ptr) Type(val).

Contoh

// allocator_construct.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 1 ; i <= 7 ; i++ )
   {
      v1.push_back( 3 * i );
   }

   cout << "The original vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   allocator<int>::pointer v1PtrA;
   int kA = 6, kB = 7;
   v1PtrA = v1Alloc.address( *find( v1.begin( ), v1.end( ), kA ) );
   v1Alloc.destroy ( v1PtrA );
   v1Alloc.construct ( v1PtrA , kB );

   cout << "The modified vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;
}
The original vector v1 is:
( 3 6 9 12 15 18 21 ).
The modified vector v1 is:
( 3 7 9 12 15 18 21 ).

batalkan alokasi

Membebaskan jumlah objek tertentu dari penyimpanan yang dimulai pada posisi tertentu.

void deallocate(pointer ptr, size_type count);

Parameter

Ptr
Penunjuk ke objek pertama yang akan dibatalkan alokasinya dari penyimpanan.

count
Jumlah objek yang akan dibatalkan alokasinya dari penyimpanan.

Keterangan

Fungsi anggota membebaskan penyimpanan untuk array objek hitungan jenis Type yang dimulai di ptr, dengan memanggil operator delete(ptr). Ptr pointer harus telah dikembalikan sebelumnya oleh panggilan untuk mengalokasikan untuk objek alokator yang dibandingkan dengan *ini, mengalokasikan objek array dengan ukuran dan jenis yang sama. deallocate tidak pernah melemparkan pengecualian.

Contoh

Untuk contoh menggunakan fungsi anggota, lihat alokator::alokasikan.

menghancurkan

Memanggil destruktor objek tanpa membatalkan alokasi memori tempat objek disimpan.

void destroy(pointer ptr);

Parameter

Ptr
Pointer yang menunjuk alamat objek yang akan dihancurkan.

Keterangan

Fungsi anggota menghancurkan objek yang ditunjuk oleh ptr, dengan memanggil destruktor ptr->Type::~Type.

Contoh

// allocator_destroy.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 1 ; i <= 7 ; i++ )
   {
      v1.push_back( 2 * i );
   }

   cout << "The original vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   allocator<int>::pointer v1PtrA;
   int kA = 12, kB = -99;
   v1PtrA = v1Alloc.address( *find(v1.begin( ), v1.end( ), kA) );
   v1Alloc.destroy ( v1PtrA );
   v1Alloc.construct ( v1PtrA , kB );

   cout << "The modified vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;
}
The original vector v1 is:
( 2 4 6 8 10 12 14 ).
The modified vector v1 is:
( 2 4 6 8 10 -99 14 ).

difference_type

Jenis integral yang ditandatangani yang dapat mewakili perbedaan antara nilai pointer dengan jenis objek yang dikelola oleh alokator.

typedef ptrdiff_t difference_type;

Keterangan

Jenis bilangan bulat yang ditandatangani menjelaskan objek yang dapat mewakili perbedaan antara alamat dari dua elemen dalam urutan yang dapat dialokasikan oleh objek jenis allocator .

Contoh

// allocator_diff_type.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 0 ; i <= 7 ; i++ )
   {
      v1.push_back( i * 2 );
   }

   cout << "The original vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   allocator<int>::const_pointer v1PtrA, v1PtrB;
   const int kA = 4, kB =12;
   v1PtrA = v1Alloc.address( kA );
   v1PtrB = v1Alloc.address( kB );
   allocator<int>::difference_type v1diff = *v1PtrB - *v1PtrA;

   cout << "Pointer v1PtrA addresses " << *v1PtrA << "." << endl;
   cout << "Pointer v1PtrB addresses " << *v1PtrB <<  "." << endl;
   cout << "The difference between the integer's addresses is: "
        << v1diff << "." << endl;
}
The original vector v1 is:
( 0 2 4 6 8 10 12 14 ).
Pointer v1PtrA addresses 4.
Pointer v1PtrB addresses 12.
The difference between the integer's addresses is: 8.

max_size

Mengembalikan jumlah elemen jenis Type yang dapat dialokasikan oleh objek alokator kelas sebelum memori bebas habis.

size_type max_size() const;

Tampilkan Nilai

Jumlah elemen yang dapat dialokasikan.

Contoh

// allocator_max_size.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 1 ; i <= 7 ; i++ )
   {
      v1.push_back( i );
   }

   cout << "The original vector v1 is:\n ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   vector <double> v2;
   vector <double> ::iterator v2Iter;
   vector <double> :: allocator_type v2Alloc;
   allocator<int>::size_type v1size;
   v1size = v1Alloc.max_size( );

   cout << "The number of integers that can be allocated before\n"
        << " the free memory in the vector v1 is used up is: "
        << v1size << "." << endl;

   int ii;
   for ( ii = 1 ; ii <= 7 ; ii++ )
   {
      v2.push_back( ii * 10.0 );
   }

   cout << "The original vector v2 is:\n ( " ;
   for ( v2Iter = v2.begin( ) ; v2Iter != v2.end( ) ; v2Iter++ )
      cout << *v2Iter << " ";
   cout << ")." << endl;
   allocator<double>::size_type v2size;
   v2size = v2Alloc.max_size( );

   cout << "The number of doubles that can be allocated before\n"
        << " the free memory in the vector v2 is used up is: "
        << v2size << "." << endl;
}

operator=

Menetapkan satu objek alokator ke objek alokator lain.

template <class Other>
    allocator<Type>& operator=(const allocator<Other>& right);

Parameter

right
Objek alokator yang akan ditetapkan ke objek lain seperti itu.

Tampilkan Nilai

Referensi ke objek alokator

Keterangan

Operator penetapan templat tidak melakukan apa pun. Namun, secara umum, objek alokator yang ditetapkan ke objek alokator lain harus dibandingkan dengan objek tersebut dan mengizinkan intermix alokasi objek dan membebaskan antara dua objek alokator.

Contoh

// allocator_op_assign.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

class Int {
public:
   Int(int i)
   {
      cout << "Constructing " << ( void* )this  << endl;
      x = i;
      bIsConstructed = true;
   };
   ~Int( ) {
      cout << "Destructing " << ( void* )this << endl;
      bIsConstructed = false;
   };
   Int &operator++( )
   {
      x++;
      return *this;
   };
   int x;
private:
   bool bIsConstructed;
};

int main( )
{
   allocator<Int> Alloc;
   allocator<Int> cAlloc ;
   cAlloc = Alloc;
}

Pointer

Jenis yang menyediakan penunjuk ke jenis objek yang dikelola oleh alokator.

typedef value_type *pointer;

Keterangan

Jenis penunjuk menjelaskan objek ptr yang dapat menunjuk, melalui ekspresi *ptr, objek apa pun yang dapat dialokasikan oleh objek jenis allocator .

Contoh

// allocator_ptr.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <int> v1;
   vector <int>::iterator v1Iter;
   vector <int>:: allocator_type v1Alloc;

   int i;
   for ( i = 1 ; i <= 7 ; i++ )
   {
      v1.push_back( 3 * i );
   }

   cout << "The original vector v1 is:\n( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ; v1Iter++ )
      cout << *v1Iter << " ";
   cout << ")." << endl;

   allocator<int>::const_pointer v1Ptr;
   const int k = 12;
   v1Ptr = v1Alloc.address( k );

   cout << "The integer addressed by v1Ptr has a value of: "
        << "*v1Ptr = " << *v1Ptr << "." << endl;
}
The original vector v1 is:
( 3 6 9 12 15 18 21 ).
The integer addressed by v1Ptr has a value of: *v1Ptr = 12.

pengikatan ulang

Struktur yang memungkinkan alokator untuk objek dari satu jenis untuk mengalokasikan penyimpanan untuk objek jenis lain.

struct rebind { typedef allocator<_Other> other; };

Parameter

Lain
Jenis elemen tempat memori dialokasikan.

Keterangan

Struktur ini berguna untuk mengalokasikan memori untuk jenis yang berbeda dari jenis elemen kontainer yang sedang diimplementasikan.

Templat kelas anggota menentukan jenis lainnya. Tujuan tunggalnya adalah untuk memberikan nama allocator<_Other>jenis , mengingat nama allocator<Type>jenis .

Misalnya, mengingat objek al alokator jenis A, Anda dapat mengalokasikan objek jenis _Other dengan ekspresi:

A::rebind<Other>::other(al).allocate(1, (Other *)0)

Atau, Anda dapat menamai jenis penunjuknya dengan menulis jenis:

A::rebind<Other>::other::pointer

Contoh

// allocator_rebind.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef vector<int>::allocator_type IntAlloc;
int main( )
{
   IntAlloc v1Iter;
   vector<int> v1;

   IntAlloc::rebind<char>::other::pointer pszC =
      IntAlloc::rebind<char>::other(v1.get_allocator()).allocate(1, (void *)0);

   int * pInt = v1Iter.allocate(10);
}

referensi

Jenis yang menyediakan referensi ke jenis objek yang dikelola oleh alokator.

typedef value_type& reference;

Keterangan

Jenis referensi menjelaskan objek yang dapat menunjuk objek apa pun yang dapat dialokasikan oleh objek jenis allocator .

Contoh

// allocator_reference.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <double> v;
   vector <double> ::iterator vIter, vfIter;
   vector <double> :: allocator_type vAlloc;

   int j;
   for ( j = 1 ; j <= 7 ; j++ )
   {
      v.push_back( 100.0 * j );
   }

   cout << "The original vector v is:\n ( " ;
   for ( vIter = v.begin( ) ; vIter != v.end( ) ; vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;

   vfIter = v.begin( );
   allocator<double>::reference vref =*vfIter;
   cout << "The value of the element referred to by vref is: "
        << vref << ",\n the first element in the vector." << endl;

   // nonconst references can have their elements modified
   vref = 150;
   cout << "The element referred to by vref after being modified is: "
        << vref << "." << endl;
}
The original vector v is:
( 100 200 300 400 500 600 700 ).
The value of the element referred to by vref is: 100,
the first element in the vector.
The element referred to by vref after being modified is: 150.

size_type

Jenis integral yang tidak ditandatangani yang dapat mewakili panjang urutan apa pun yang dapat dialokasikan objek jenis allocator .

typedef size_t size_type;

Contoh

// allocator_size_type.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>

using namespace std;

int main( )
{
   vector <double> v;
   vector <double> ::iterator vIter;
   vector <double> :: allocator_type vAlloc;

   int j;
   for ( j = 1 ; j <= 7 ; j++ )
   {
      v.push_back( 100.0 * j );
   }

   cout << "The original vector v is:\n ( " ;
   for ( vIter = v.begin( ) ; vIter != v.end( ) ; vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;

   allocator<double>::size_type vsize;
   vsize = vAlloc.max_size( );

   cout << "The number of doubles that can be allocated before\n"
        << " the free memory in the vector v is used up is: "
        << vsize << "." << endl;
}

value_type

Jenis yang dikelola oleh alokator.

typedef Type value_type;

Keterangan

Jenisnya adalah sinonim untuk parameter Typetemplat .

Contoh

// allocator_value_type.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>
using namespace std;

int main( )
{
   vector <double> v;
   vector <double> ::iterator vIter, vfIter;
   vector <double> :: allocator_type vAlloc;

   int j;
   for ( j = 1 ; j <= 7 ; j++ )
   {
      v.push_back( 100.0 * j );
   }

   cout << "The original vector v is:\n ( " ;
   for ( vIter = v.begin( ) ; vIter != v.end( ) ; vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;

   vfIter = v.begin( );
   allocator<double>::value_type vecVal = 150.0;
*vfIter = vecVal;
   cout << "The value of the element addressed by vfIter is: "
        << *vfIter << ",\n the first element in the vector." << endl;

   cout << "The modified vector v is:\n ( " ;
   for ( vIter = v.begin( ) ; vIter != v.end( ) ; vIter++ )
      cout << *vIter << " ";
   cout << ")." << endl;
}
The original vector v is:
( 100 200 300 400 500 600 700 ).
The value of the element addressed by vfIter is: 150,
the first element in the vector.
The modified vector v is:
( 150 200 300 400 500 600 700 ).

Pembantu

allocator_arg_t

struct allocator_arg_t { explicit allocator_arg_t() = default; };
inline constexpr allocator_arg_t allocator_arg{};

uses_allocator

template <class T, class Alloc> struct uses_allocator;