#include <map>
#include <iostream>
#include <memory>
#include <limits>
template<typename T>
class MyAllocator{
public :
// typedefs
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public :
// convert an allocator<T> to allocator<U>
template<typename U>
struct rebind {
typedef MyAllocator<U> other;
};
public :
MyAllocator() {}
template<typename U>
MyAllocator(MyAllocator<U> const&) {}
// address
pointer address(reference r) { return &r; }
const_pointer address(const_reference r) { return &r; }
// memory allocation
pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0) {
std::cout<<"Trying to allocate "<<cnt<<" objects in memory"<<std::endl;
pointer new_memory = reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
std::cout<<"Allocated "<<cnt<<" objects in memory at location:"<<new_memory<<std::endl;
return new_memory;
}
void deallocate(pointer p, size_type n) {
::operator delete(p);
std::cout<<"Deleted "<<n<<" objects from memory"<<std::endl;
}
// size
size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}
// construction/destruction
void construct(pointer p, const T& t) {
std::cout<<"Constructing at memory location:" <<p<<std::endl;
new(p) T(t);
}
void destroy(pointer p) {
std::cout<<"Destroying object at memory location:" <<p<<std::endl;
p->~T();
}
bool operator==(MyAllocator const&) { return true; }
bool operator!=(MyAllocator const& a) { return !operator==(a); }
}; // end of class MyAllocator
template<typename Key, typename T>
using Map = std::map<Key, T, std::less<Key>, MyAllocator<std::pair<const Key, T>>>;
int main() {
typedef Map<int, int> Inner;
typedef Map<int, Inner> Outer;
Inner m_inner;
Outer m_outer;
m_inner[0] = 1234;
m_inner[1] = 1234;
m_inner[2] = 1234;
m_outer[1] = m_inner;
std::cout << "hahaha " << m_outer[1][0] << std::endl;
return 0;
}