如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串
How to delete a single string in an array with deleteEntry function c++
这是我要实现的功能
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;
}
相关文章:
- 将字符向量转换为单个字符串
- 如何在 c++ 中将 2D 数组转换为单个字符串
- 将单个字符串/字符输入串行监视器
- 查找单个字符串中重复次数最多和次数第二多的字符
- 如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串
- 如何从空格分隔的单个字符串中提取多个子字符串
- 如何比较C++中的单个字符串字符?
- 在 C++98 中写入单个字符串以"线程安全"吗
- 如何将字符串和整数组合成单个字符串 (C++)
- 如何将单个字符串与字符串数组进行比较
- 如何获取从GetPrivateProfileSection()返回的单个字符串
- 将任意数量的任意类型的值组合为单个字符串的简单命令
- 如何在 c++ 中将文件中的段落作为单个字符串读取
- 将字符串数组与用户输入的单个字符串进行比较,它还表明它在该位置找到 2 次
- 在hashmap中搜索,其中键可以是单个字符串或空格分隔的字符串
- 管理单个字符串的内存
- 从字符串中获取单个字符串字符
- 指针——单个字符串与字符串数组
- 在单个字符串中显示Unicode QString的QLatin1String连接
- 如何检查单个字符串输入的部分是int还是char