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

标签 c++ arrays dynamic-arrays

下面是我要实现的功能

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; 
} 

关于c++ - 如何使用 deleteEntry 函数删除数组中的单个字符串 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48489241/

相关文章:

c++ - 你如何找到网格物体的高度?

c++ - 如何阻止在 C++ 中从开始销毁循环中创建的对象?

java - 如何在Java中读取数组变量

c - 如何在运行时将任何类型存储在 void* 中?

c - 将二进制文件 fread 到动态分配的 C 数组中

c++ - C++ union 中的命名结构

c++ - Visual Studio 2017 控制台应用程序 : precompiled header

arrays - ElasticSearch:嵌套数组与单独类型

arrays - 在 bash 函数中定义一个本地数组并在该函数外部访问它

c - 尝试释放 2D 动态分配数组时程序崩溃