我正在练习POO
但有指针,我必须在所请求的位置插入另一个数组,而不会覆盖或丢失那些,只需移动它。
在函数void IntArr::addElement(int pos,int qtty,int *vec)
中,我就是这么做的,但正如您将看到的那样,我的生活复杂化了。
是否有更有效的方法来避免这么多行代码?
一些参考文献:
qtty
是我想从矢量传递到插入的元素数。
pos
是我要插入的位置。
vec
是要插入的载体
p
动态向量,我将插入值
used
向量p的已用元素数量
size
矢量大小p
我留下了函数的代码,但是如果你需要完整的类和主要来理解我需要的东西,请告诉我并编辑它。
这是我的代码:
#include <iostream>
#include <string.h>
#include <iostream>
#include "IntArr.h"
using std::cout;
using std::endl;
#define PRESS_KEY std::cout<<"\nPresione Enter para continuar . . .\n";std::cin.get();
#define SZ_VEC(x) (sizeof(x)/sizeof(x[0]))
class IntArr
{
private:
int * p;
int size;
int used;
//Verificador
void redimensionador(int cant);
void verificarPos(int &pos);
public:
//builders
IntArr (int sz);
IntArr (int sz,int qtty,int *vec);
//Destroyer
~IntArr();
//Print
void prtArr (void) const;
//Gets
int getSize(){return size;};
int getUsed(){return used;};
//Adds
void addElement(int pos,int xx);
void addElement(int pos,int qtty,int *vec);
};
//Builders
IntArr::IntArr(int sz){
size = sz;
used = 0;
p = new int[size];
}
IntArr::IntArr(int sz,int qtty,int* vec){
if(qtty>sz){
sz = qtty;
}
size = sz;
used = qtty;
p = new int[size];
for(int i=0;i<used;i++){
p[i] = vec[i];
}
}
//Destroyer
IntArr::~IntArr(){
delete []p;
}
//Print
void IntArr::prtArr(void) const{
if(used == 0){
cout<<endl<<"El array no tiene elementos."<<endl;
}
else{
cout<<endl<<"Array: ";
for(int i=0;i<used;i++){
cout<<p[i]<<", ";
}
cout<<endl;
}
}
//Add value 'xx' in position 'pos'
void IntArr::addElement(int pos,int xx){
verificarPos(pos);
redimensionador(1);
for(int i=used;i>pos;i--){
p[i] = p[i-1];
}
p[pos] = xx;
used++;
}
//Add Vector in position 'pos'
void IntArr::addElement(int pos,int qtty,int *vec){
verificarPos(pos);
redimensionador(qtty);
// Iterator of auxiliary vectors
int j=0;
// We see the amount of elements to move
int cantDesp;
cantDesp = used - pos;
// We create the auxiliary vector with the amount of elements to be moved
int vAux[cantDesp];
// Go through the original array to save the elements to be moved
for(int i=pos;i<used;i++){
vAux[j] = p[i];
j++;
}
// We see where the new items arrive to add
int espNew;
espNew = pos + qtty;
j=0; // Restart iterator
// We add the new items to the vector p[]
for(int i=pos;i<espNew;i++){
p[i] = vec[j];
j++;
}
// We calculate the pos up to where the displaced items will go
int pAux;
pAux = used + qtty;
j=0;
// We use the vAux to add saved items
for(int i=used-1;i<pAux;i++){
p[i] = vAux[j];
j++;
}
used += qtty;
}
//Resize
void IntArr::redimensionador(int cant){
if(cant+used>size){
if(cant > 5){
size += cant;
}
else{
size += 5 + cant;
}
int *temp = new int [size];
memcpy (temp,p,used*sizeof(int));
delete [] p;
p = temp;
}
}
//Checker of position
void IntArr::verificarPos(int &pos){
if(pos<=0){ // If the position is negative or equal to zero
pos = 0;
}
else if(pos>=size){ // If the position exceeds the size of the array
pos = used;
}
}
int main(int argc, char *argv[]){
int v1[]={0,5,10,15,20,25,30,35,40};
int v2[]={1,2,3,4,5,6};
IntArr A(10,SZ_VEC(v1),v1);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
A.addElement(0,77);
A.addElement(56,11);
A.addElement(4,SZ_VEC(v2),v2);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
A.addElement(4,99);
cout<<" size:"<<A.getSize()<<endl<<" used:"<<A.getUsed()<<endl;
A.prtArr();
PRESS_KEY;
}