fork(2) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include <map>
  5. #include <iostream>
  6. #include <vector>
  7. #include <memory>
  8. #include <scoped_allocator>
  9. #include <typeinfo>
  10. #include <type_traits>
  11. #include <algorithm>
  12. #include <random>
  13. #include <iostream>
  14. #include <functional>
  15. #include <set>
  16. #include <typeindex>
  17. #include <unordered_map>
  18. #include <ctime>
  19. #include <iterator>
  20.  
  21. template<typename T>
  22. class MyAllocator{
  23. public :
  24. // typedefs
  25.  
  26. typedef T value_type;
  27. typedef value_type* pointer;
  28. typedef const value_type* const_pointer;
  29. typedef value_type& reference;
  30. typedef const value_type& const_reference;
  31. typedef std::size_t size_type;
  32. typedef std::ptrdiff_t difference_type;
  33.  
  34. public :
  35. // convert an allocator<T> to allocator<U>
  36.  
  37. template<typename U>
  38. struct rebind {
  39. typedef MyAllocator<U> other;
  40. };
  41.  
  42. public :
  43. inline explicit MyAllocator() {}
  44. inline ~MyAllocator() {}
  45. inline explicit MyAllocator(MyAllocator const&) {}
  46. template<typename U>
  47. inline explicit MyAllocator(MyAllocator<U> const&) {}
  48.  
  49. // address
  50.  
  51. inline pointer address(reference r) { return &r; }
  52. inline const_pointer address(const_reference r) { return &r; }
  53.  
  54. // memory allocation
  55.  
  56. inline pointer allocate(size_type cnt,
  57. typename std::allocator<void>::const_pointer = 0) {
  58. std::cout<<"Trying to allocate "<<cnt<<" objects in memory"<<std::endl;
  59. pointer new_memory = reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
  60. std::cout<<"Allocated "<<cnt<<" objects in memory at location:"<<new_memory<<std::endl;
  61. return new_memory;
  62. }
  63. inline void deallocate(pointer p, size_type n) {
  64. ::operator delete(p);
  65. std::cout<<"Deleted "<<n<<" objects from memory"<<std::endl;
  66. }
  67. // size
  68. inline size_type max_size() const {
  69. return std::numeric_limits<size_type>::max() / sizeof(T);
  70. }
  71.  
  72. // construction/destruction
  73.  
  74. inline void construct(pointer p, const T& t) {
  75. std::cout<<"Constructing at memory location:" <<p<<std::endl;
  76. new(p) T(t);
  77. }
  78. inline void destroy(pointer p) {
  79. std::cout<<"Destroying object at memory location:" <<p<<std::endl;
  80. p->~T();
  81. }
  82.  
  83. inline bool operator==(MyAllocator const&) { return true; }
  84. inline bool operator!=(MyAllocator const& a) { return !operator==(a); }
  85. }; // end of class MyAllocator
  86.  
  87. int main() {
  88.  
  89. typedef std::map<int, int, std::less<int>, MyAllocator<std::pair<const int, int>>> Inner;
  90. typedef std::map<int, Inner, std::less<int>, MyAllocator<std::pair<const int, Inner>>> Outer;
  91. Inner m_inner;
  92. Outer m_outer;
  93. m_inner[0] = 1234;
  94. m_inner[1] = 1234;
  95. m_inner[2] = 1234;
  96. m_outer[1] = m_inner;
  97. std::cout << "hahaha " << m_outer[1][0] << std::endl;
  98.  
  99. return 0;
  100. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9853008
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9853028
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9853048
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9853068
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9853098
Constructing at memory location:0x9853098
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x98530b8
Constructing at memory location:0x98530b8
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x98530d8
Constructing at memory location:0x98530d8
hahaha 1234
Destroying object at memory location:0x9853068
Destroying object at memory location:0x98530b8
Deleted 1 objects from memory
Destroying object at memory location:0x9853098
Deleted 1 objects from memory
Destroying object at memory location:0x98530d8
Deleted 1 objects from memory
Deleted 1 objects from memory
Destroying object at memory location:0x9853048
Deleted 1 objects from memory
Destroying object at memory location:0x9853028
Deleted 1 objects from memory
Destroying object at memory location:0x9853008
Deleted 1 objects from memory