-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyVector.cpp
More file actions
112 lines (82 loc) · 2.19 KB
/
MyVector.cpp
File metadata and controls
112 lines (82 loc) · 2.19 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
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
#include "MyVector.h"
#include "Generales.h"
#include <iostream>
using namespace std;
using namespace UTEC;
MyVector::MyVector() {
vector = nullptr;
dimension = 0;
}
MyVector::MyVector(TipoEntero nElementos) {
dimension = nElementos;
vector = new TipoDataVector[dimension];
}
MyVector::~MyVector() {
delete [] vector; //Los enteros (int) se liberan solos, por lo que no se necesita un for.
}
void MyVector::push_back(TipoDataVector valor) {
TipoDataVector* auxiliar;
auxiliar = new TipoDataVector[dimension+1];
for(TipoEntero i=0;i<dimension;i++){
auxiliar[i] = vector[i];
}
delete [] vector;
vector = auxiliar;
vector[dimension++] = valor;
}
TipoNumerico MyVector::size() {
return dimension;
}
void MyVector::insert(TipoEntero posicion, TipoDataVector valor) {
vector[posicion] = valor;
}
void MyVector::pop_back() {
TipoDataVector* auxiliar;
auxiliar = new TipoDataVector[dimension-1];
for(TipoEntero i=0;i<dimension-1;i++){
auxiliar[i]=vector[i];
}
delete[] vector;
vector = auxiliar;
dimension--;
}
void MyVector::erase(TipoEntero posicion) {
TipoDataVector* auxiliar;
auxiliar = new TipoDataVector[dimension-1];
for (TipoEntero i = 0; i < dimension-1; i++) {
if(i>=posicion)
auxiliar[i] = vector[i+1];
}
delete[] vector;
vector = auxiliar;
dimension--;
}
TipoDataVector MyVector::operator[](TipoEntero n) {
return vector[n];
}
MyVector MyVector::operator+(MyVector _vector) {
const TipoEntero _dimension = dimension;
TipoEntero newDimension = _dimension+(_vector.size());
MyVector newVector(newDimension);
for (TipoEntero i = 0; i<_dimension; i++) {
newVector.insert(i,vector[i]);
}
for(TipoEntero i = _dimension;i<newDimension;i++){
newVector.insert(i,_vector[i-_dimension]);
}
return newVector;
}
void MyVector::fill() {
for (TipoEntero i=0;i<dimension;i++){
vector[i] = 0;
}
}
void MyVector::print() {
for (TipoEntero i=0;i<dimension;i++){
cout << vector[i];
if(i != dimension-1) {
cout << " - ";
}else
cout<< "\n";
}
}