fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <string>
  4. #include <type_traits>
  5.  
  6. template <typename T>
  7. struct NoisyAlloc
  8. {
  9. using size_type = std::size_t;
  10. using difference_type = std::ptrdiff_t;
  11.  
  12. using value_type = T;
  13. using pointer = T *;
  14. using const_pointer = const T *;
  15. using reference = T &;
  16. using const_reference = const T &;
  17.  
  18. template <typename U> struct rebind { using other = NoisyAlloc<U>; };
  19.  
  20. T * allocate(std::size_t n)
  21. {
  22. std::cout << "Allocating " << n << " units.\n";
  23. return static_cast<T *>(::operator new(n * sizeof(T)));
  24. }
  25. void deallocate(T * p, std::size_t n)
  26. {
  27. std::cout << "Freeing " << n << " units.\n";
  28. ::operator delete(p);
  29. }
  30.  
  31. using is_always_equal = std::true_type;
  32. bool operator==(const NoisyAlloc&) const { return true; }
  33. bool operator!=(const NoisyAlloc&) const { return false; }
  34. };
  35.  
  36.  
  37. int main()
  38. {
  39. std::set<unsigned char> s;
  40. for (unsigned int c = 0; c < 0x100; ++c) s.insert(c);
  41.  
  42. std::basic_string<char, std::char_traits<char>, NoisyAlloc<char>> str(s.begin(), s.end());
  43.  
  44. //std::basic_string<char, std::char_traits<char>, NoisyAlloc<char>> str;
  45. //str.reserve(500);
  46. //str.append(s.begin(), s.end());
  47. //str.assign(s.begin(), s.end());
  48. //str.insert(str.end(), s.begin(), s.end());
  49. //for (unsigned char c : s) str.push_back(c);
  50. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Allocating 269 units.
Freeing 269 units.