vector.h
1.25 KB
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
#ifndef __VECTOR_H__
#define __VECTOR_H__
template<class C> class vector
{
C *data;
unsigned int size;
public:
class index_out_of_range{};
explicit vector (int s)
{
data = new C[s]();
size = s;
}
~vector ()
{
delete [] data;
}
C& operator[] (unsigned int pos)
{
if (pos >= size)
throw index_out_of_range();
return data[pos];
}
C operator[] (unsigned int pos) const
{
if (pos >= size)
throw index_out_of_range();
return data[pos];
}
vector (const vector<C> & s)
{
data = new C[s.size];
size = s.size;
try {
for (unsigned i = 0; i < size; i++)
data[i] = s.data[i];
}
catch(...)
{
delete [] data;
throw;
}
}
void swap(vector<C>& s)
{
C* t1=s.data;
unsigned int t2=s.size;
s.data=data;
s.size=size;
data=t1;
size=t2;
}
vector<C> & operator= (const vector<C> & s)
{
if (this == &s)
return *this;
vector<C> n(s);
swap(n);
return *this;
}
friend ostream & operator<< (ostream & o, const vector<C> & v)
{
o << '[';
for (unsigned i = 0; i < v.size;i++)
{
o << v[i];
if (i != v.size - 1)
o << ',';
};
o << ']';
return o;
}
};
#endif /* __VECTOR_H__ */