首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于给动态数组删除元素的有关问题

2013-12-04 
关于给动态数组删除元素的问题代码如下。编译没有问题,但是当运行到Would you like to delete some element

关于给动态数组删除元素的问题
代码如下。编译没有问题,但是当运行到
Would you like to delete some elements?
的时候,就会出错跳出,检查了半天也不知道是什么原因。

这个程序很简单,最主要的就是deleteEntry这个函数,该函数将在结束时删除原动态数组,并返回一个新动态数组。三个参数中,第一个参数是原动态数组的名字;第二个是其维数,因为删除元素后会变,所以用了call-by-reference传递类型;最后一个是要删除的元素(如果没找到这个元素,那么返回原来的动态数组)。


//
#include <iostream>
#include <string>


inline void keep_window_open()
{
using std::cout;
cout << "\nPress any key to exit:";
getchar();
}

std::string* deleteEntry(std::string *dynamicArray, int& size, std::string newEntry);
// Precondition: size is the SIZE of dynamicArray[], a dynamically allocated array
// Postcondition: newEntry, if can be found in dynamicArray, is deleted,
// while all of the other elements are preserved in a new array

void display(std::string *array, const int size);
// Postcondition: as its name implies

int main()
{
using namespace std;
int size;
cout<<"Input the size of the original string array:\t";
cin>>size;
while(size <= 0)
{
cout<<"\nThe size must be a positive integer!\n";
cin>>size;
}
string *sArray=new string [size];
for(int i=0; i < size; ++i)
{
cout<<"The "<<i+1<<"th string:\t";
cin>>sArray[i];
}
string *newArray;
newArray=sArray;
cout<<"\nThis is the array you gave:\n";
display(newArray,size);

char decision;
cout<<"\nWould you like to delete some elements?\n";
cin>>decision;
if(decision != 'n' && decision != 'N')
{
cout<<"\nDetermine which element you want to delete from it:\n";
string newEntry;
while(cin>>newEntry)
{
newArray=deleteEntry(newArray,size,newEntry);
cout<<"\nAfter the deletion, it becomes:\n";
display(newArray,size);
cout<<endl;
}
cin.clear();

}

keep_window_open();
}

std::string* deleteEntry(std::string* dynamicArray, int& size, std::string newEntry)
{
std::string *newArray;
int original_size = size;
bool match = false;
for(int i=0; i < original_size; ++i)
{
if(dynamicArray[i]==newEntry)
{
--size;
if(match == false)
match=true;
}
}
newArray=new std::string [size];
if(match == false)
{
newArray=dynamicArray;
}
else
{
for(int j=0,k=0; j<size; ++j)
{
while(k<original_size)
{
if(dynamicArray[k] != newEntry)
{
newArray[j]=dynamicArray[k];
++k;
break;
}
else
++k;
}
}
}
delete []dynamicArray;
return newArray;
}

void display(std::string *array, const int size)
{
for(int i=0; i != size; ++i)
std::cout<<array[i]<<std::endl;
}
动态数组 删除
[解决办法]
引用:
Quote: 引用:

运行了下,如果输入正确的删除字符串就没问题,如果不存在的就会有问题。原因在这里:
if(match == false)
{    
    newArray=dynamicArray;//这是一种浅层拷贝,它们是共同指向一块堆空间的。
}
else
{
    ……
}
delete []dynamicArray;//你这里delete掉了,那么newArray也会被delete掉
这样说应该清楚了吧,你自己调整下吧。

还是不会啊。这是一道习题,要求是deleteEntry这个函数参数名如前所示,并且要删除原来数组,返回一个新数组。难道说这题无解?

指针用=的话,改变的是指针所指向的地址,不会把里面的内容拷贝过去的。这样说你明白了么?
你要自己用循环一项项的拷贝过去,而不是 a=b;懂吗?比如:

for(int i=0;i<size;i++)
    newArray[i]=dynamicArray[i];

[解决办法]
引用:
Quote: 引用:



好吧,我大概明白了,总结起来就是下面几点,你看对不对?

1、如果p,q是两个同类型的指针,那么
p=q
后,如果删除了q所指的内容,那么p所指的内容也被清空了,p变成了一个dangling pointer,原因在于上面的语句使得p和q是同一个指针。

2、一个指针p所指的内容被删除后,仍旧可以对其进行赋值,这就是两个函数调用语句可以有效的原因。

3、采用“值复制”的方法,可以避免1中出现的问题。

1、p=q,那么两个指针共享的是一块空间,所以q对内容的操作,会影响到p。

2、指针的赋值和里面的内容无关,它保存是地址,不是该地址保存的值。不管你返回的地址是否有内容都可以赋给另外一个指针,但是无效的地址,是很危险的操作。

3、基本是这样。
[解决办法]
这里有个浅拷贝和深拷贝的问题,你可以看看这篇文章:http://blog.csdn.net/lijun5635/article/details/16852645
就是对象里有一个指针成员,在复制的时候产生的问题,你可以去看看。

热点排行