我有一个char data[len]
从二进制文件中读取的解压缩数据填充。我知道data
只能是这些类型:我知道代表 ( )char, uchar, short, ushort, int, uint, float, double
所需的确切位数。elesize = {8, 16, 32, 64}
我只想遍历数据列表,比如说,找到max()
给min()
定数字的出现次数。我想在不为内存空间问题创建另一个数组的情况下做到这一点。
我想出了以下内容,但是例如对于len == 34560000
所以我想知道是否有人有“单线”或更有效的方法来做到这一点(C 或 C++)。
char data[len];
double mymax = -std::numeric_limits<double>::max()
for (size_t i=0; i<len; i += elesize)
{
double x;
char *r = data+i;
if (elementtype == "char")
x = static_cast<double>(*r);
else if (elementtype == "uchar")
x = static_cast<double>(*((unsigned char *)r));
else if (elementtype == "short")
x = static_cast<double>(*((int16_t *)r));
else if (elementtype == "ushort")
x = static_cast<double>(*((uint16_t *)r));
else if (elementtype == "int")
x = static_cast<double>(*((int32_t *)r));
else if (elementtype == "uint")
x = static_cast<double>(*((uint32_t *)r));
else if (elementtype == "float")
x = static_cast<double>(*((float *)r));
else if (elementtype == "double")
x = *((double *)r);
if (x > mymax)
mymax = x;
}