-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.h
67 lines (50 loc) · 1.37 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
template<typename T>
class Vector
{
private:
size_t m_size = 0;
size_t m_capacity = 0;
T* m_data = nullptr;
void realloc(size_t new_size){
T* tmp = m_data;
m_data = new T[new_size];
for(size_t i=0; i<m_size; i++)
m_data[i] = std::move(tmp[i]);
m_capacity = new_size;
delete[] tmp;
}
public:
Vector(){
realloc(2);
}
~Vector(){
delete[] m_data;
}
void push_back(const T& val){
if(m_capacity <= m_size)
realloc(m_capacity*2);
m_data[m_size++] = std::move(val);
}
void push_back(T&& val){
if(m_capacity <= m_size)
realloc(m_capacity*2);
m_data[m_size++] = std::move(val);
}
int size(){
return m_size;
}
T& operator[] (size_t idx){
return m_data[idx];
}
};
struct my_int {
int x;
my_int(){}
my_int(int x) : x(x) {}
my_int(const my_int& other) : x(other.x) { std::cout << "copied" << std::endl; }
my_int(my_int&& other) : x(other.x) { std::cout << "moved" << std::endl; }
my_int& operator=(const my_int& other) { std::cout << "copied" << std::endl; x = other.x; return *this; }
my_int& operator=(my_int&& other) { std::cout << "moved" << std::endl; x = other.x; return *this; }
~my_int(){ std::cout << "destroyed" << std::endl; }
};