函数通过元素指向第一个元素指向的数组
Function to move through an array pointed by the first element by element
我正在尝试循环遍历数组的元素,对于这些元素,我必须调用一个函数指向的函数,并传递了元素地址,以及在最后一个参数中存储的地址。该功能返回该函数指向返回true的元素数量。这是我一直在尝试遵循的要求,但我无法确切地做我要做的事情。
------要求&功能-------------
/* Write an enumeration function named sum() with the following parameters: a generic pointer an int that holds the number of elements in the array pointed to an int that holds the size in bytes of a single element a pointer to a function that has two generic pointer parameters and returns a bool a generic pointer Your function moves through the array pointed to by the first parameter element by element. For each element, your function calls the function pointed to and passes the element's address along with the address stored in the last parameter. Your function returns the number of elements for which the function pointed to returned true. Since your first function parameter is a generic pointer and your function can handle any type, you will need to cast the address of the input array to the address of a chars in order to move from one element to the next. */
int sum(void* x,int n,int s,bool( f)(void ,void*),void* z){
char *arr = static_cast<char*>(x); int count = 0; for (; s < n-2; s++){ arr += s; count += f(arr, z); } if (n / 1 == n) return count; else if (n % 2 == 0) return count; else return 0;
}
我希望你们可以向我展示和解释,或者至少我没有以适当的方式做的事情。.我真的很感激这是唯一的方法,我可以学习.. :)
如果需要更多解释,请告诉我..
** * *** 添加****** ***
/* Write a callback function named isEven() with the following parameters: a generic pointer to an input value a generic pointer to an output value Your function works with ints and returns true if the input value is even, false otherwise. Moreover, if the value is even, your function adds the value to that pointed to by the second parameter. */ bool isEven(void* x, void* z){ int a = *static_cast<int*>(x); int b = *static_cast<int*>(z); if (a % 2 == 0){ // finding even numbers b += a; return true; } else { return false; } } /* Write another callback function named isPrime() with the following parameters: a generic pointer to an input value a generic pointer to an output value Your function works with ints and returns true if the input value is a prime number, false otherwise. Moreover, if the value is prime, your function adds the value to that pointed to by the second parameter. */ bool isPrime(void* x, void* z){ int a = *static_cast<int*>(x); int b = *static_cast<int*>(z); if ((a / 1 == a) && (a / a == 1)){ // finding prime numbers b += a; return true; } else { return false; } }
** * ** * **** 预期输出* ** * ** * ** ***********
5 evens found in {1,2,3,4,5,6,7,8,9,10,11} sum is 30
5 primes found in {1,2,3,4,5,6,7,8,9,10,11} sum is 28
如果我正确理解了该功能看起来为
int sum( const void* x, int n, int s, bool(*f)( const void*, const void* ), const void* z )
{
const char *p = reinterpret_cast<const char *>( x );
int count = 0;
for ( int i = 0; i < n; i++ )
{
count += f( p, z );
p += s;
}
return count;
}
这是使用该函数
的示例#include <iostream>
#include <cstdlib>
#include <ctime>
int sum( const void* x, int n, int s, bool(*f)( const void*, const void* ), const void* z )
{
const char *p = reinterpret_cast<const char *>( x );
int count = 0;
for ( int i = 0; i < n; i++ )
{
count += f( p, z );
p += s;
}
return count;
}
bool lt( const void *p1, const void *p2 )
{
return ( *reinterpret_cast<const int *>( p1 ) <
*reinterpret_cast<const int *>( p2 ) );
}
int main()
{
std::srand( ( unsigned int )std::time( 0 ) );
const int N = 10;
int a[N];
for ( int &x : a ) x = std::rand() % N;
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
int x = 5;
int n = sum( a, N, sizeof( int ), lt, &x );
std::cout << "There are " << n << " elements less than " << x << std::endl;
return 0;
}
样本输出
4 7 2 9 2 8 6 1 9 9
There are 4 elements less than 5
char* arr = (char*) x;
for(; s < (n-1); s++)
{
f((void*) &arr[s], z);
// or
f((void*) (arr + s * sizeof(char)), z);
}
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 如何将元素添加到数组的线程安全函数?
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 输出没有重复元素的动态数组(收缩数组)C++
- 数组中最大的非重复元素
- 数组元素打印的递归方法
- 对字符数组中的元素执行逐位操作
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过交换元素使数组相同
- 按平均值替换数组中的元素
- 打印矢量数组中的所有元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 如何将元素从向量转移到新数组?
- 如何在向量数组中插入元素?
- 为什么 2 个相同数组的元素彼此相等
- C++ 中的二维整数数组,每行中的元素数量不均匀