编写程序从数组中删除重复项
To write a program to remove duplicates from an array
我正在研究从数组中删除重复项的程序,我在这里使用三个函数:一个用于输入大小和数字,第二个函数用于删除重复项并返回没有重复项的数字,第三个函数只是一个报告显示大小和新数字,但我有问题,我不知道在报告或菲利普错误中我认为在哪一步:
函数
‘int main()’
:从‘int*’
到‘int’
的转换无效,初始化‘void report(int, int)’
的参数1
#include <iostream>
using namespace std;
const int size = 100;
void phillip(int[], int & );
/* Preconditions: Array of base type in declared and int varuable declared
postconditions: the array is filled with values supllied by the user at
the keybord. the user is assked how many values they want - this value is
given to the second argument.
*/
int remdubs(int[], int noel);
/* Preconditions: An array of basetype int that has noel values.
postconditions: The number of unique elemts in the array is returned. The function removes all dubplicates inside the array.
*/
void report(int s, int d);
int main()
{
int ruby[size];
int numele, numuniq;
phillip(ruby, numele);
numuniq = remdubs(ruby, numele);
report(ruby, numuniq);
return 0;
}
void phillip(int[], int& )
{
int s;
cout << "nHow many values you want? ";
cin >> s;
cout << "nPlease input 10 integers, hitting return after each one n";
for (int i = 0; i < s; i++)
{
int num;
cin >> num;
}
}
int rembups(int sapphire[], int noel)
{
for (int i = 0; i < noel; i++)
{
for (int j = i + 1; j < noel; j++)
{
if (sapphire[i] == sapphire[j])
{
for (int k = j; k < noel; k++)
sapphire[k] = sapphire[k + 1];
noel--;
j--;
}
}
}
return noel;
}
void report(int s, int d)
{
cout << "nYou entered " << s << "distinct numbers: " << d;
}
没有比你的错误更好的解释了:
void report (int s, int d);
这个函数要求两个整数值,你正在传递一个数组给它,它的功能衰减了,将表现得像一个整数指针
int ruby[size];
report (ruby, numuniq);
我不确定你的程序的行为,但你应该做一些像
report(ruby[0], numuniq);
即:访问数组中的元素并将其提供给函数
错误是在函数报告中,它要求两个整数,而您正在传递一个数组和一个整数我认为在函数报告中不需要两个参数一个就可以了
void report(int d)
{
cout << "nYou entered <<d<<" distinct numbers";
}
可以解决你的错误,但我不认为你会得到你想要的输出
代码的几个问题:
-
Report
函数应该期望一个整数数组,而不是单个整数 - 尝试使用人类可读的变量名或函数名
-
remdubs
在函数定义 中重命名为 - phillip函数定义没有正确定义参数
- 你有
const int size
,但不要使用它 -
remdups
有一个bug。我将把它留在这里,因为我正试图解决其他问题。 - 有比
remdups
更好的方法来查找副本。请考虑其他解决方案。
rempubs
我已经修复了你的代码,并提供了它在ideone
相关文章:
- 你能帮我解决删除数组 c++ 中重复值的挑战吗?
- 删除[i] 数组和删除数组 [i] 之间的区别
- 如何实现删除数组的功能?
- 删除数组 c++ 崩溃问题
- 如何删除数组列表中的元素
- C++删除数组只会解除分配第一项
- 删除数组成员的更好方法是什么?
- 如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串
- 删除数组中所有出现的数字 C++
- C++ 删除数组时检测到堆损坏
- 不允许删除数组的一部分,为什么
- 如何删除数组中第一次出现的值
- 当我删除数组时,指针在哪里?指向数组的指针与数组中的元素之间的区别
- 尝试用C 中的空地址(而非null)删除数组指针
- 为什么 std::remove 不删除数组的最后一个元素
- 如何使用c++删除数组中的一些值
- 仅删除数组的第一个元素
- 删除数组时断言失败
- 如何并行删除数组中的零值
- 访问指针数组/通过另一个指针并删除数组中的单个元素而不删除第一个指针或整个数组