fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdexcept>
  4.  
  5. std::vector<int> testVector;
  6.  
  7. void SetVector(int position, int value) {
  8. try
  9. {
  10. testVector.at(position) = value;
  11. }
  12. catch (const std::out_of_range& oor) {
  13. testVector.reserve(position);
  14. testVector[position] = value;
  15. }
  16. }
  17.  
  18. int main()
  19. {
  20. std::cout << testVector.capacity() << std::endl;
  21. SetVector(1, 1);
  22. std::cout << testVector[1] << std::endl;
  23. SetVector(1, 2);
  24. std::cout << testVector[1] << std::endl;
  25. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
0
1
2