about summary refs log tree commit diff
path: root/cpptour/Vector.cc
blob: 8f943458e9050ae2c6ae819be8cf0c24d694e347 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdexcept>

#include "Vector.h"

using namespace std;

Vector::Vector (int s)
{
  if (s < 0)
    throw length_error{"You're being negetive!"};
  elem = new double[s];
  sz = s;
}

Vector::Vector (initializer_list<double> lst)
: elem {new double[lst.size()]}, sz {static_cast<int> (lst.size())}
{
  copy(lst.begin(), lst.end(), elem);
}

Vector::~Vector ()
{
  delete[] elem;
}

int
Vector::size () noexcept
{
  return sz;
}

double&
Vector::operator[] (int i)
{
  if (i < 0 || size() <= i)
    throw out_of_range{"Vector::operator[]"};
  return elem[i];
}