-
Notifications
You must be signed in to change notification settings - Fork 3
/
alloc_construct.cpp
67 lines (51 loc) · 2.23 KB
/
alloc_construct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <vector>
#include <iostream>
template <typename T>
struct MyAlloc
{
typedef unsigned size_type;
typedef int difference_type;
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef typename std::add_lvalue_reference<value_type>::type reference;
typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
template <class U> struct rebind {typedef MyAlloc<U> other;};
MyAlloc() noexcept {}
~MyAlloc() {}
pointer allocate(size_type n, const void* = 0)
{ return (pointer)::operator new(n * sizeof(value_type)); }
void deallocate(pointer p, size_type)
{ ::operator delete((void*)p);}
};
template <>
struct MyAlloc<int>
{
typedef unsigned size_type;
typedef int difference_type;
typedef int value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef typename std::add_lvalue_reference<value_type>::type reference;
typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
template <class U> struct rebind {typedef MyAlloc<U> other;};
MyAlloc() noexcept {}
~MyAlloc() {}
pointer allocate(size_type n, const void* = 0)
{ return (pointer)::operator new(n * sizeof(value_type)); }
void deallocate(pointer p, size_type)
{ ::operator delete((void*)p);}
void construct(pointer p)
{ *p = 3; }
void destroy(pointer p) {}
};
int main () {
std::vector<int, std::allocator<int>> v1;
v1.resize(5);
for (int i: v1) std::cout << i << ' ';
std::cout << std::endl;
std::vector<int, MyAlloc<int>> v2;
v2.resize(5);
for (int i: v2) std::cout << i << ' ';
std::cout << std::endl;
}