【发布时间】:2016-07-31 19:19:56
【问题描述】:
我还是 C++ 新手,所以这对我来说是一个学习过程。我也知道我最初应该使用向量来执行此操作,但我有一个指定数组的练习,所以我正在尝试编写一个函数来删除数组中的所有重复元素,但我收到错误
C2100:非法间接
如果有人能指出我正确的方向
int main()
{
int *t;
int removel[9] = { 1, 1, 1, 2, 3, 4, 5, 6, 6, };
t = removeAll(removel, 9, 1);
for (int i = 0; i < 8; i++)
cout << t[i] << " ";
}
int* removeAll(int list[], int listlength, int removeitem)
{
int count = 0;
int* list2;
int removeindex;
int length;
int tempindex;
for (int i = 0; i < listlength; i++)
{
if (removeitem == list[i])
count++;
}
length = listlength - (count + 1);
list2 = new int[length];
int j;
while (j<=length)
{
remove_if(list[0], list[listlength - 1], removeitem);
for (j = 0; j < length; j++)
if (list[j] == NULL)// not sure what the remove_if func puts inplace of the removed element
continue;
else
list2[j] = list[j];
}
return list2;
}
【问题讨论】:
-
#1 不要在需要变量之前声明它们。
-
使用向量。我看不出您为什么要使用数组。并且数组是固定大小的。
-
这是我正在使用的教科书的练习,只是想学习如何完成这项任务。
-
切换到向量后,考虑一下:stackoverflow.com/questions/36384571/…
-
好的,#2 格式化你的代码,你希望能够阅读它。 #3
j未初始化。 #4 阅读reference #5 思考算法。