#include <iostream>
using namespace std;
#include <map>
#include <iostream>
#include <vector>
#include <memory>
#include <scoped_allocator>
#include <typeinfo>
#include <type_traits>
#include <algorithm>
#include <random>
#include <iostream>
#include <functional>
#include <set>
#include <typeindex>
#include <unordered_map>
#include <ctime>
#include <iterator>
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 :
inline explicit MyAllocator() {}
inline ~MyAllocator() {}
inline explicit MyAllocator(MyAllocator const&) {}
template<typename U>
inline explicit MyAllocator(MyAllocator<U> const&) {}
// address
inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }
// memory allocation
inline 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;
}
inline void deallocate(pointer p, size_type n) {
::operator delete(p);
std::cout<<"Deleted "<<n<<" objects from memory"<<std::endl;
}
// size
inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}
// construction/destruction
inline void construct(pointer p, const T& t) {
std::cout<<"Constructing at memory location:" <<p<<std::endl;
new(p) T(t);
}
inline void destroy(pointer p) {
std::cout<<"Destroying object at memory location:" <<p<<std::endl;
p->~T();
}
inline bool operator==(MyAllocator const&) { return true; }
inline bool operator!=(MyAllocator const& a) { return !operator==(a); }
}; // end of class MyAllocator
int main() {
typedef std::map<int, int, std::less<int>, MyAllocator<std::pair<const int, int>>> Inner;
typedef std::map<int, Inner, std::less<int>, MyAllocator<std::pair<const 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;
}