fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <memory>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. template < typename T >
  9. struct MyAllocator : std::allocator< T > {
  10.  
  11. // ctors
  12. MyAllocator() = default;
  13. MyAllocator( MyAllocator const & ) = default;
  14. template < typename U >
  15. MyAllocator( MyAllocator<U> const & ) { }
  16.  
  17. // member function
  18. typename std::allocator< T >::pointer
  19. allocate( typename std::allocator< T >::size_type n,
  20. std::allocator<void>::const_pointer hint = 0 ) {
  21.  
  22. typename std::allocator< T >::pointer result = std::allocator< T >::allocate( n, hint );
  23.  
  24. cout << "allocate(" << n << ", " << hint << ") -> " << result << " "
  25. << "size = " << sizeof( T ) * n << endl;;
  26.  
  27. return result;
  28. }
  29.  
  30. // member type
  31. template < typename U >
  32. struct rebind {
  33. typedef MyAllocator<U> other;
  34. };
  35.  
  36. };
  37.  
  38. int main() {
  39.  
  40. typedef int iIndex;
  41. typedef std::string Data;
  42.  
  43. typedef map<
  44. iIndex,
  45. Data,
  46. std::less< iIndex >,
  47. MyAllocator< std::pair<const iIndex, Data> >
  48. > mapData;
  49.  
  50. typedef int iType;
  51. typedef map<
  52. iType,
  53. mapData,
  54. std::less< iType >,
  55. MyAllocator< std::pair<const iType, mapData> >
  56. > Foo;
  57. Foo foo;
  58.  
  59. foo[ 1 ].insert( make_pair(1, "aaa") );
  60.  
  61. }
  62.  
Success #stdin #stdout 0s 3024KB
stdin
Standard input is empty
stdout
allocate(1, 0) -> 0x93c1020 size = 44
allocate(1, 0) -> 0x93c1050 size = 24