如何将 std::map 值复制到非容器数组中
How to copy std::map value into non-container array?
我有一个填充了id和浮点数组的地图。我想将浮点数(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;
});
我试图使用 std::copy 来做到这一点,我认为需要一个 lambda 才能将 tmp.seconds 的映射放入temp_arr,但到目前为止,还没有。
std::copy
在这里不合适。 相反,您可以使用std::transform
:
std::transform(temps.begin(), temps.end(), temp_arr,
[](const std::pair<const int, Temp>& entry) {
return entry.second;
});
相关文章:
- std::向量与传递值的动态数组
- std::vector的包装器,使数组的结构看起来像结构的数组
- 初始化具有非默认构造函数的std::数组项的更好方法
- 在c++中尝试对对象数组进行排序时,出现std:bad_alloc错误
- 将 int 数组转换为 std::vector<int*>
- 输入std::数组时出现问题
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 为什么std::vector比数组慢
- C++如何生成std::数组列表
- 捕获lambda中的std::数组
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 将无符号字符的向量存储在数组中会给我 std::bad_alloc
- 当 std::move 与 C 样式数组或不移动对象时会发生什么
- std::数组边界检查如何工作?
- 通过unique_ptr访问std::数组
- 我正在使用 std::bitset 并尝试创建两个大小为 100,000,000,000 的数组 std::bitset
- 对象的数组-STD ::数组 - 构造函数初始化问题
- 标准::数组<std::array<T、N2>、N> 数据成员上的括号运算符重载
- 从未来对象数组std::vector中检索值
- 如何将动态数组(std::vector)传递给odbc函数SQLBindParameter