-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimple_vector.h
38 lines (28 loc) · 1.08 KB
/
simple_vector.h
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
#pragma once
#include <algorithm>
#include <initializer_list>
// a very simplified vector of doubles
class vector {
public:
// constructor: allocate s elements, let elem point to them, store s in sz
explicit vector(int s) :sz(s), elem(new double[s]{0}) {}
// initializer-list constructor
vector(std::initializer_list<double> lst) :sz(lst.size()), elem(new double[sz]) {
std::copy(lst.begin(), lst.end(), elem); // initialize (using std::copy())
}
// copy constructor: define copy
vector(const vector& v) :sz(v.sz), elem(new double[sz]) {
std::copy(v.elem, v.elem + sz, elem);
}
vector(vector&& v);
// destructor: free memory
~vector() { delete[] elem; }
vector& operator=(const vector& v);
vector& operator=(vector&& v);
int size() const { return sz; } // the current size
double& operator[](int i) { return elem[i]; } // for non-const vectors
double operator[](int i) const { return elem[i]; } // for const vectors
private:
int sz; // the size
double* elem; // pointer to the elements
};