Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. class IndexError {};
  2.  
  3.  
  4. class Vector {
  5. int capacity_;
  6. int size_;
  7. int *buffer_;
  8. public:
  9. Vector (int capacity=10): capacity_(capacity), size_(capacity) {
  10. buffer_ = new int[capacity];
  11. }
  12.  
  13. ~Vector () {
  14. delete [] buffer_;
  15. }
  16.  
  17. int size() const {
  18. return size_;
  19. }
  20.  
  21. bool empty() const {
  22. return size_ == 0;
  23. }
  24.  
  25. int &index(n) const {
  26. if (n < 0 || n > size_)
  27. throw IndexError();
  28. return buffer_[n];
  29. }
  30.  
  31. int &operator [](int n) {
  32. return index(n);
  33. }
  34.  
  35. const int &operator [](int n) const {
  36. return index(n);
  37.  
  38. }
  39.  
  40. void clear() {
  41. size_ = 0;
  42. }
  43.  
  44. int capacity() const {
  45. return capacity_;
  46. }
  47.  
  48. int &front() {
  49. if (empty())
  50. throw IndexError();
  51. return buffer_[0];
  52. }
  53.  
  54. const int &front() const {
  55. return front();
  56. }
  57.  
  58.  
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement