fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct MyType {};
  5.  
  6. int main()
  7. {
  8. const int NumberToPushBack = 10000;
  9.  
  10. std::vector<MyType> array;
  11. int numberOfReallocations = 0;
  12.  
  13. for(int i = 0; i < NumberToPushBack; ++i)
  14. {
  15. size_t oldSize = array.capacity();
  16. array.push_back(MyType());
  17.  
  18. if(oldSize != array.capacity())
  19. ++numberOfReallocations;
  20. }
  21.  
  22. std::cout << "In " << NumberToPushBack << " push_backs, there were only " << numberOfReallocations << " reallocations of the buffer." << std::endl;
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
In 10000 push_backs, there were only 15 reallocations of the buffer.