我在测试中有以下数据:
2011-01-03 2116
2011-01-03 2120
2011-01-04 2116
2011-01-04 2115
和下面的代码:
std::map<std::string, std::vector<double> >::iterator tk = test.begin();
std::vector<double>tmp;
std::copy(tk->second.begin(), tk->second.end(), std::back_inserter(tmp));
上面的代码 tmp
包含:
2116
2120
2116
2115
但是,我想将每个日期的 tk->second
的平均值插入到 tmp
中。我是否必须将我的 back_inserter 写入循环?
最佳答案
for(auto it = test.begin(); it != test.end(); it++)
{
double sum = 0.0;
int count = 0;
for(auto it2 = it->second.begin(); it2 != it->second.end(); it2++, count++)
{
sum += *it2;
}
tmp.push_back(sum / count);
}
关于c++ - 将数据的平均值插入 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10463584/