本文实例为大家分享了C++实现动态顺序表的具体代码,供大家参考,具体内容如下
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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
#pragma once #include <stdio.h> #include <iostream> #include <assert.h> #include <string.h> using namespace std; typedef int DataType; class Vector { public : Vector() :_first(NULL) , _finish(NULL) , _endofstorage(NULL) {} Vector( const Vector& v) { if (v.Size() > 0) { _first = new DataType[v.Size()]; //只开辟原有数据所占空间大小,节省空间 memcpy (_first, v._first, sizeof (DataType)*v.Size()); if (_first) { _finish = _first + v.Size(); _endofstorage = _first + v.Size(); } else { _first = _finish = _endofstorage = NULL; } } } Vector& operator=(Vector& v) { if ( this != &v) { ////传统写法 //DataType* tmp = new DataType[v.Size()]; //memcpy(tmp, _first, sizeof(DataType)*v.Size()); //delete[] _first; //_first = tmp; //_finish = _first + v.Size(); //_endofstorage = _first + v.Size(); //现代写法 swap(_first, v._first); swap(_finish, v._finish); swap(_endofstorage, v._endofstorage); } return * this ; } ~Vector() { delete [] _first; _first = _finish = _endofstorage = NULL; } void Print() { DataType* cur = _first; while (cur != _finish) { cout << *cur << " " ; ++cur; } cout << endl; } size_t Size() const ; size_t Capacity() const ; void Expand( size_t n); void PushBack(DataType x); void Reserve( size_t n); void PopBack(); void Insert( size_t pos, DataType x); void Erase( size_t pos); size_t Find(DataType x); private : DataType* _first; DataType* _finish; DataType* _endofstorage; }; size_t Vector::Size() const { return _finish - _first; } size_t Vector::Capacity() const { return _endofstorage - _first; } void Vector::Expand( size_t n) { if (n > Capacity()) { size_t size = Size(); DataType* tmp = new DataType[n]; memcpy (tmp, _first, sizeof (DataType)*size); delete [] _first; _first = tmp; _finish = _first + size; //切记更新新的_finish和_endofstorage _endofstorage = _first + n; } } void Vector::PushBack(DataType x) { //if (_finish == _endofstorage) //{ // if (Capacity() == 0) // { // Expand(3); // } // else // { // Expand(Capacity() * 2); // } //} //*_finish = x; //++_finish; Insert(Size(), x); } void Vector::Reserve( size_t n) { if (n > Capacity()) { Expand(n); } } void Vector::PopBack() { assert (_finish > _first); --_finish; } void Vector::Insert( size_t pos, DataType x) { assert (pos <= Size()); if (_finish == _endofstorage) { if (Capacity() == 0) { Expand(3); } else { Expand(Capacity() * 2); } } int end = Size() - 1; while (end >= ( int )pos) { _first[end + 1] = _first[end]; --end; } _first[pos] = x; ++_finish; } void Vector::Erase( size_t pos) { assert (pos < Size()); size_t cur = pos; while (cur < Size() - 1) { _first[cur] = _first[cur + 1]; ++cur; } --_finish; } size_t Vector::Find(DataType x) { DataType* cur = _first; while (cur != _finish) { if (*cur == x) { return cur - _first; } ++cur; } return -1; } void TestVector() { Vector v1; v1.PushBack(1); v1.PushBack(2); v1.PushBack(3); v1.PushBack(4); v1.Print(); size_t pos = v1.Find(2); printf ( "pos expect 1,actual %lu\n" , pos); Vector v2(v1); v2.Insert(0, 0); v2.Print(); Vector v3; v3 = v2; v3.Print(); v3.Erase(1); v3.Print(); } |
test.cpp
1
2
3
4
5
6
7
8
|
#include "Vector.h" int main() { cout << "顺序表:" << endl; TestVector(); return 0; } |
效果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/adorable_/article/details/80086254