如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串

How to delete a single string in an array with deleteEntry function c++

本文关键字:单个 字符串 数组 删除 何使用 deleteEntry 函数 c++      更新时间:2023-10-16

这是我要实现的功能

string* deleteEntry(string* dynamicArray, int& size, string entryToDelete);
// Precondition: dynamicArray point to a array of strings with give size,
//               newEntry is a string
// Postcondition: The function should search dynamicArray for entryToDelete.
//                If not found, the request should be ignored and the
//                unmodified dynamicArray returned. If found, create a new
//                dynamic array one element smaller than dynamicArray. Copy
//                all element except entryToDelete into the new array, delete
//                dynamicArray, decrement size, and return the new dynamic
//                array
string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
{
     for (int i=0;i<size,i++);
     { 
         if (entryToDelete==dynamicArray[i])
         {
             delete[] entryToDelete;
         }
     }
}          

显然,我是一个初学者,我不是想让你写我的代码,而是给我关于如何删除条目的建议。我在我的教科书中找不到它,我在网上找到的所有示例都包含一个单独的函数来完成这个,我觉得这可以在单个函数中完成。

首先,您的for循环格式不正确。您需要一个包含逗号的分号,并且需要删除尾随分号。

改变

for (int i=0;i<size,i++);

for (int i=0;i<size;i++)

其次,您不能delete[]数组中的单个条目,因为它们不是单独分配的new[]开始(只有整个数组是

(。

至于你的问题,答案就在你代码的注释中。您只是没有遵循概述的步骤:

// Postcondition: The function should search dynamicArray for entryToDelete.
// If not found, the request should be ignored and the
// unmodified dynamicArray returned. If found, create a new
// dynamic array one element smaller than dynamicArray. Copy
// all element except entryToDelete into the new array, delete
// dynamicArray, decrement size, and return the new dynamic
// array

您需要注意您的要求并按照他们所说的去做,例如:

string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
{
    // The function should search dynamicArray for entryToDelete...
    for (int i = 0; i < size; ++i);
    {
        if (entryToDelete == dynamicArray[i]) // If found...
        {
            // create a new dynamic array one element smaller than dynamicArray...
            string *newArray = new string[size-1];
            // Copy all element except entryToDelete into the new array...
            for(int j = 0; j < i; ++j)
                newArray[j] = dynamicArray[j];
            for(int j = i+1; j < size; ++j)
                newArray[j-1] = dynamicArray[j];
            // delete dynamicArray...
            delete[] dynamicArray;
            // decrement size...
            --size;
            // return the new dynamic array...
            return newArray; 
        }
    }
    // If not found, return the unmodified dynamicArray...
    return dynamicArray; 
} 

如果删除数组元素是指删除指针数组中元素指向的对象:

删除我的阵列[elemen];

但是,如果它不是一个指针数组,并且通过删除数组元素,您的意思是通过删除中间的某些元素来缩小数组,这在C++中无法完成。

我强烈建议使用STL提供的各种数据结构和算法。创建 STL 的原因有很多,其中之一是实际上消除了创建自己的静态和动态数组的需要,从而避免了随之而来的所有问题。下面的代码演示如何使用 STL 完成任务。

vector<string> myStrings = {"erase this string"};
string stringToErase = "erase this string";
auto it = find(myStrings.begin(), myStrings.end(), stringToErase);
if (it != myStrings.end())
    myStrings.erase(it);

如果你有兴趣精通现代C++,那么Stroustrup,Meyers和Sutter等人的一两本书在这方面会很有帮助。

//函数从数组中删除指定的条目string* deleteEntry(string* dynamicArray, int&size, string entryToDelete({ 字符串 *结果 = 动态数组;

int indexofEntry = -1;
for(int i=0;i<size; i++)
{
    if(dynamicArray[i] == entryToDelete)
    {
        indexofEntry = i;
        break;
    }
}
if (indexofEntry == -1)
    return result;
else
{
    result = new string[size-1];
    int indexNewArray = 0;
    for(int i=0; i<size; i++)
    {
        if(i != indexofEntry)
        {
            result[indexNewArray] = dynamicArray[i];
            indexNewArray++;
        }
        else
        {
            //i=indexofEntry
        }
    }
    size--;
    delete[] dynamicArray;
}
return result;

}