实现一个名为 find 的函数,该函数将以下参数作为参数(按此顺序):
我们要在数组中找到的对象 ANY 类型的动态数组 数组的大小 此函数应在数组中查找指定的元素并返回该元素的索引位置。如果元素不存在,该函数应返回 -1。
我拥有的代码是:
template<typename t>
t find(t objectInArray, t *array, int arraySize)
{
array = new t[arraySize];
for(int index = 0; index < arraySize; index++){
if(array[index] == objectInArray){
cout << index;
return index;
}
}
return -1;
}
我使用 int 在 main 中运行了这段代码,没有模板,它运行良好。我实际上很困惑我的代码有什么问题。