-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.cpp
170 lines (168 loc) · 3.74 KB
/
vector.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class Element {
private:
int number;
public:
Element() :number(0) {
cout << "ctor" << endl;
}
Element(int num):number(num) {
cout << "ctor" << endl;
}
Element(const Element& e):number(e.number) {
cout << "copy ctor" << endl;
}
Element(Element&& e):number(e.number) {
cout << "right value ctor" << endl;
}
~Element() {
cout << "dtor" << endl;
}
void operator=(const Element& item) {
number = item.number;
}
bool operator==(const Element& item) {
return (number == item.number);
}
void operator()() {
cout << number ;
}
int GetNumber() {
return number;
}
};
template<typename T>
class Vector {
private:
T* items;
int count;
public:
Vector() :count{ 0 }, items{nullptr} {
}
Vector(const Vector& vector) :count{vector.count} {
items = static_cast<T*>(malloc(sizeof(T) * count));
memcpy(items, vector.items, sizeof(T) * count);
}
//构造函数
Vector(Vector&& vector) :count{ vector.count }, items{ vector.items } {
vector.items = nullptr;
vector.count = 0;
}
//析构函数
~Vector() {
this->Clear();
}
T& operator[](int index){
if (index<0||index>=count) {
cout<<"invalid index"<<endl;
return items[0];
}
return items[index];
}
int returnCount(){
return count;
}
//析构全部元素
void Clear() {
for(int i=0;i<count;i++){
items[i].~T();
}
count = 0;
free(items);
items = nullptr;
}
//push_back实现
void Add(const T& item) {
T* nitems = static_cast<T*>(malloc(sizeof(T)*(count+1)));
for(int i=0;i<count;i++){
new (nitems+i) T(move(items[i]));
}
new (nitems+count) T(item);
int ncount = count+1;
this->Clear();
count = ncount;
items = nitems;
}
//插入元素
bool Insert(const T& item,int index) {
if(index<0||index>count)return false;
T* nitems = static_cast<T*>(malloc(sizeof(T)*(count+1)));
for(int i=0;i<index;i++){
new (nitems+i) T(move(items[i]));
}
new (nitems+index) T(item);
for(int i=index;i<count;i++){
new (nitems+i+1) T(move(items[i]));
}
int ncount = count+1;
this->Clear();
count = ncount;
items = nitems;
return true;
}
//移除下标为index的元素
bool Remove(int index) {
if(index<0||index>=count)return false;
T* nitems = static_cast<T*>(malloc(sizeof(T)*(count-1)));
for(int i=0;i<index;i++){
new (nitems+i) T(move(items[i]));
}
for(int i=index+1;i<count;i++){
new (nitems+i-1) T(move(items[i]));
}
int ncount = count-1;
this->Clear();
count = ncount;
items = nitems;
return true;
}
//查看是否包含元素item,如果包含则返回索引
int Contains(const T& item) {
for(int i=0;i<count;i++){
if(items[i]==item)
return i;
}
return -1;
}
};
template<typename T>
void PrintVector(Vector<T>& v){
int count=v.returnCount();
for (int i = 0; i < count; i++)
{
v[i]();
cout << " ";
}
cout << endl;
}
int main() {
Vector<Element>v;
for (int i = 0; i < 4; i++) {
Element e(i);
v.Add(e);
}
PrintVector(v);
Element e2(4);
if (!v.Insert(e2, 10))
{
v.Insert(e2, 2);
}
PrintVector(v);
if (!v.Remove(10))
{
v.Remove(2);
}
PrintVector(v);
Element e3(1), e4(10);
cout << v.Contains(e3) << endl;
cout << v.Contains(e4) << endl;
Vector<Element>v2(v);
Vector<Element>v3(move(v2));
PrintVector(v3);
v2.Add(e3);
PrintVector(v2);
return 0;
}