2012-05-05 43 views
0

我在測試以下數據:插入數據的平均到載體

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寫入循環嗎?

回答

1
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); 
}