我有一些数据存储在大小为“M”的一维数组中。现在我需要将其视为维度为 NxP 的二维数组,其中 N 和 P 的乘积等于 M。我仅在运行时知道 N 和 P 的值。我怎样才能在C中实现这样的功能?
int array[M]; /* one dimensional array where some data is stored*/
int** newArray; /* the dimension of newArray should be NxP such that we can access the data in 'array' as a two-dimensional array*/
Run Code Online (Sandbox Code Playgroud) 我希望编写一个C++模板函数,该函数反过来使用一些"C"函数并利用函数重载.
例如,我需要myAbs使用模板编写一个函数,这些模板根据输入参数类型进行适当的调用fabs或abs定义math.h.这该怎么做?
#include <math.h>
template<typename T>
T abs(T x)
{
// I need to write an efficient code here!
// If it is 'double' and 'float' I may be able to compare the
// sizeof(Type) and call 'return fabs(x)' or 'return abs(x)'.
// But this is not a good solution as two types can be of same size!
}
Run Code Online (Sandbox Code Playgroud)
注意:我只是用它作为例子来解释我的问题.我已经知道这样的功能"abs"已经可用了<cmath>.