fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. bool reallocation_check(const int*& data, const std::vector<int>& v)
  5. {
  6. if (data != v.data())
  7. {
  8. std::cout << "Reallocation at size: " << v.size() << "\nNew capacity: " << v.capacity() << '\n';
  9. data = v.data();
  10. return true;
  11. }
  12.  
  13. return false;
  14. }
  15.  
  16. int main()
  17. {
  18. const unsigned limit = 83886;
  19.  
  20. std::vector<int> v;
  21. const int* data = v.data();
  22. unsigned reallocations = 0;
  23.  
  24.  
  25. for (unsigned i = 0; i < limit; ++i)
  26. {
  27. v.push_back(i + 100);
  28. if (reallocation_check(data, v))
  29. ++reallocations;
  30. }
  31.  
  32. std::cout << "Reallocations: " << reallocations << '\n';
  33. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
Reallocation at size: 1
New capacity: 1
Reallocation at size: 2
New capacity: 2
Reallocation at size: 3
New capacity: 4
Reallocation at size: 5
New capacity: 8
Reallocation at size: 9
New capacity: 16
Reallocation at size: 17
New capacity: 32
Reallocation at size: 33
New capacity: 64
Reallocation at size: 65
New capacity: 128
Reallocation at size: 129
New capacity: 256
Reallocation at size: 257
New capacity: 512
Reallocation at size: 513
New capacity: 1024
Reallocation at size: 1025
New capacity: 2048
Reallocation at size: 2049
New capacity: 4096
Reallocation at size: 4097
New capacity: 8192
Reallocation at size: 8193
New capacity: 16384
Reallocation at size: 16385
New capacity: 32768
Reallocation at size: 32769
New capacity: 65536
Reallocation at size: 65537
New capacity: 131072
Reallocations: 18