fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class C
  5. {
  6. int ID = 0;
  7.  
  8. public:
  9. C(const int newID)
  10. {
  11. ID = newID;
  12. }
  13.  
  14. int getID()
  15. {
  16. return ID;
  17. }
  18. };
  19.  
  20. int main() {
  21. std::vector<C> pack;
  22. pack.reserve(10);
  23. printf("pack has %i\n", pack.size()); //will print '0'
  24. pack[4] = C(57);
  25. printf("%i\n", pack[4].getID()); //will print '57'
  26. printf("pack has %i\n", pack.size()); //will still print '0'
  27. pack.at(4) = C(57); // exception
  28.  
  29. }
Runtime error #stdin #stdout #stderr 0s 4556KB
stdin
Standard input is empty
stdout
pack has  0
57
pack has  0
stderr
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 4) >= this->size() (which is 0)