vector.h
1.24 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
#ifndef __VECTOR_H__
#define __VECTOR_H__
template<class C> class vector
{
C *dane;
unsigned int size;
public:
class index_out_of_range{};
explicit vector (int s)
{
dane = new C[s]();
size = s;
}
~vector ()
{
delete [] dane;
}
C& operator[] (unsigned int pos)
{
if (pos >= size)
throw index_out_of_range();
return dane[pos];
}
C operator[] (unsigned int pos) const
{
if (pos >= size)
throw index_out_of_range();
return dane[pos];
}
vector (const vector<C> & s)
{
dane = new C[s.size];
size = s.size;
try {
for (unsigned i = 0; i < size; i++)
dane[i] = s.dane[i];
}
catch(...)
{
delete [] dane;
throw;
}
}
void swap(vector<C>& s)
{
C* t1=s.dane;
unsigned int t2=s.size;
s.dane=dane;
s.size=size;
dane=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__ */