从四个给定的无符号字符数组中计算平均值数组 [英] Calculate average array from four given unsigned char arrays

查看:68
本文介绍了从四个给定的无符号字符数组中计算平均值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有四个未签名的char数组,它们被添加到如下所示的map容器中:

Suppose I have a toatl of four unsigned char arrays, which are added into a map container like the following:

std::map<std::string, unsigned char*> UCArray;
UCArray.insert(std::make_pair("A1", new unsigned char[10000])); 
UCArray.insert(std::make_pair("A2", new unsigned char[10000])); 
UCArray.insert(std::make_pair("A3", new unsigned char[10000])); 
UCArray.insert(std::make_pair("A4", new unsigned char[10000])); 


我想得到一个基于UCArray的平均值填充的数组,其中所有四个未签名的char缓冲区都被初始化并填充了有效值.我知道我可以通过使用两个"for ... loop"来表达我的观点.请告知是否还有其他有效方法可以做到这一点.预先谢谢您.


I want to get an array filled with average values based on UCArray, in which all four unsigned char buffers are initialized and populated with valid values. I know that I can carry my point through the use of two "for ... loop". Would please tell if there is any other effective way(s) to do that. Thank you in advance.

推荐答案

不,没有比使用两个循环更好的方法了,除非您接受在第一个索引上展开循环.

无论如何,您最好避免在内部循环内为数组建立索引,因为这将使您重复查找地图40000次的开销!
最好保留一个指向四个缓冲区的指针数组并为它们建立索引.

No, there is no better way than using two loops, except if you accept unrolling the loop on the first index.

Anyway, you''d better avoid indexing the array inside the inner loop as this would cost you the overhead of the map lookup repeated 40000 times !

It is much better to keep an array of pointers to the four buffers and index them.

// Retrieve the buffers
unsigned char* Buffer[4];
Buffer[0]= UCArray["A1"];
Buffer[1]= UCArray["A2"];
Buffer[2]= UCArray["A3"];
Buffer[3]= UCArray["A4"];

for (int i= 0; i < 10000; i++)
{
    int Avg= 0;
    Avg+= Buffer[0][i];
    Avg+= Buffer[1][i];
    Avg+= Buffer[2][i];
    Avg+= Buffer[3][i];
    Avg/= 4;

    // Store the average
    ...
}


顺便说一句,您也可以不用地图容器...

您没有指定是否要获得行或列的平均值.另一种情况甚至更简单(一个指针就足够了).


By the way, you could also do without the map container...

You didn''t specify if you want the row or column averages. The other case is even simpler (a single pointer suffices).


这篇关于从四个给定的无符号字符数组中计算平均值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆