C++ 在不删除现有元素的情况下,在特定位置将数组插入到另一个数组中
我正在做一个C++ 在不删除现有元素的情况下,在特定位置将数组插入到另一个数组中,c++,arrays,pointers,insert,C++,Arrays,Pointers,Insert,我正在做一个POO的练习,但是使用指针,我必须在请求的位置插入一个数组到另一个数组中,而不覆盖或丢失那些位置,只要移动它就可以了 在函数void intar::addElement(int pos,int qtty,int*vec)中,我确实做到了这一点,但正如您将看到的,我的生活复杂化了 有没有更有效的方法来避免这么多代码行 一些参考资料: qtty是要从向量传递到插入的元素数 pos是我要插入的位置 vec是要插入的向量 pdynamic vector,我将在其中插入值 已使用向量p的已使用
POO
的练习,但是使用指针,我必须在请求的位置插入一个数组到另一个数组中,而不覆盖或丢失那些位置,只要移动它就可以了
在函数void intar::addElement(int pos,int qtty,int*vec)
中,我确实做到了这一点,但正如您将看到的,我的生活复杂化了
有没有更有效的方法来避免这么多代码行
一些参考资料:
qtty
是要从向量传递到插入的元素数
pos
是我要插入的位置
vec
是要插入的向量
p
dynamic vector,我将在其中插入值
已使用
向量p的已使用元素数量
size
向量大小p
我留下了函数的代码,但是如果您需要完整的类和main来理解我需要什么,请告诉我,然后我编辑它
这是我的代码:
#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;
}
#包括
#包括
#包括
#包括“intar.h”
使用std::cout;
使用std::endl;
#定义按键std::coutw为什么不使用标准容器,例如std::vector
,std::list
?它们有插入元素的操作,负责移动元素和调整大小,如果容器类型支持,则倾向于优化。@Peter这显然是一个学习如何实现容器的教学练习。什么是POO?幸运的是,这不是一个常见的首字母缩略词。到底是什么是老师强加的障碍?@Calvin您可以学习OOP,而不必处理试图编写容器类的复杂问题。Java学生如何在不处理指针的情况下学习OOP?此外,编写一个合适的容器类不仅仅是“如何使用指针”。你的IntArr
本身是不完整的,因为{IntArray a(1);IntArray b=a;}
会让你惨败。下次我遇到OO狂热者时,我不得不提到OOP
用西班牙语表示为POO
:-)