【问题标题】:Calculate average array from four given unsigned char arrays从四个给定的无符号字符数组计算平均数组
【发布时间】:2011-07-16 12:20:24
【问题描述】:

假设我有一个由四个无符号字符数组组成的toatl,它们被添加到如下地图容器中:

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 填充平均值的数组,其中所有四个无符号字符缓冲区都被初始化并填充了有效值。我知道我可以通过使用两个“for ...循环”来表达我的观点。请告诉是否有任何其他有效的方法来做到这一点。提前谢谢你。

【问题讨论】:

  • boost::accumulatorsfor_each ?

标签: c++ arrays average


【解决方案1】:
for(std::map<std::string, unsigned char*>::const_iterator iter = UCArray.begin();
    iter != UCArray.end(); ++iter)
{
   int nAverage =  std::accumulate(
             iter->second, 
             iter->second+10000, 
             0)  / 10000;
}

【讨论】:

  • 只计算A1的平均值
  • @Ajay 谢谢!我想得到一个基于 4 个给定数组的平均数组。 unsigned char* ucAverage = new unsigned char[10000];
  • for (int i = 0;
  • @Armen,这是隐含的。我只是举了一个简化的例子。
  • @Ajay,我不能用你的例子,因为它可以用来评估一个数组的平均值,结果是整个数组的平均值。我如何使用 std::accumulate 来完成上述工作?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多