25 template <
typename ObjectTemplate>
32 explicit Vector(
int init_size = MIN_CAPACITY)
34 _capacity((init_size >= MIN_CAPACITY) ? init_size : MIN_CAPACITY) {
35 _object_template =
new ObjectTemplate[ _capacity ];
43 delete [] _object_template;
48 delete[] _object_template;
50 _capacity = rhs._capacity;
52 _object_template =
new ObjectTemplate[capacity()];
53 for(
int k = 0; k < size(); k++) {
54 _object_template[k] = rhs._object_template[k];
60 void resize(
int new_size) {
61 if(new_size > _capacity) {
62 reserve(new_size * 2 + 1);
67 void reserve(
int new_capacity) {
68 if(new_capacity < _size) {
71 ObjectTemplate *old_array = _object_template;
73 _object_template =
new ObjectTemplate[new_capacity];
74 for(
int k = 0; k < _size; k++) {
75 _object_template[k] = old_array[k];
77 _capacity = new_capacity;
81 ObjectTemplate & operator[](
int idx) {
82 return _object_template[idx];
85 const ObjectTemplate& operator[](
int idx)
const {
86 return _object_template[idx];
97 int capacity()
const {
101 void push_back(
const ObjectTemplate& x) {
102 if(_size == _capacity) {
103 reserve(2 * _capacity + 1);
105 _object_template[_size] = x;
117 const ObjectTemplate& back()
const {
118 return _object_template[_size - 1];
121 typedef ObjectTemplate* iterator;
122 typedef const ObjectTemplate* const_iterator;
125 return &_object_template[0];
128 const_iterator begin()
const {
129 return &_object_template[0];
133 return &_object_template[_size];
136 const_iterator end()
const {
137 return &_object_template[_size];
140 void erase(
int position) {
141 if(position < _size) {
142 for(
int k = position; k + 1 < _size; k++) {
143 _object_template[k] = _object_template[k + 1];
156 ObjectTemplate* _object_template;
161 #endif // M2M_VECTOR_H A simple C++ Vector class, used as replacement for std::vector.
Definition: m2mvector.h:29
Namespace defined as replace for components defined under std namespace.
Definition: m2mstring.h:24