[英]How to copy std::map value into non-container array?
I have a map that is populated with an id, and an array of floats. 我有一张地图,其中填充了一个ID和一个浮动数组。 I'd like to copy the floats (pair.second) into *temp_arr. 我想将浮点数(pair.second)复制到* temp_arr。
// Temp object
struct Temp
{
float t1, t2, t3;
float n1, n2, n3;
};
// A map that is populated.
std::map<int, Temp> temps;
// temps gets populated ...
int i = 0;
Temps *temp_arr = new Temp[9];
// currently, I do something like ...
for (auto& tmp : temps)
temp_arr[i++] = tmp.second;
// here's what I tried ...
std::for_each(temps.begin(), temps.end(), [&](std::pair<int, Temp> &tmp) {
temp_arr[i++] = tmp.second;
});
I was trying to do this using std::copy and I think a lambda would be needed to get a map of tmp.seconds into the temp_arr, but so far, not yet. 我试图使用std :: copy做到这一点,我认为需要一个lambda才能将tmp.seconds的映射映射到temp_arr中,但到目前为止,还没有。
std::copy
isn't appropriate here. std::copy
在这里不合适。 Instead, you can use std::transform
: 相反,您可以使用std::transform
:
std::transform(temps.begin(), temps.end(), temp_arr,
[](const std::pair<const int, Temp>& entry) {
return entry.second;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.