The C++ allocator() program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: To create allocator objects for constructors using allocator() in C++ programming
To show: How to use allocator(), constructors used to create allocator objects in C++ programming
// the allocator() C++ example
#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(void)
{
allocator<double> Alloc;
vector <Int>:: allocator_type vec1Alloc;
allocator<double> cAlloc(Alloc);
allocator<Int> cvec1Alloc(vec1Alloc);
if (cvec1Alloc == vec1Alloc)
cout<<"The allocator objects cvec1Alloc & vec1Alloc are equal."<<endl;
else
cout<<"The allocator objects cvec1Alloc & v1ecAlloc 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;
return 0;
}
Output example:
The allocator objects cvec1Alloc & vec1Alloc are equal.
The allocator objects cAlloc & Alloc are equal.
Press any key to continue . . .