英文:
std::transform in copying data from json array to a float array C++
问题
I have a json array as shown below
{
{ "Data":[1.0, 2.0, 3.0] }
}
I am trying to copy this json array to a float array using std::transform
.
In code -
#include <nlohmann/json.hpp>
#include <memory>
using json = nlohmann::json;
int main(){
json tensor;
tensor["Data"] = { 1.0, 2.0, 3.0 };
auto dataPtr = std::make_unique<float[]>(3);
const auto& dataVec = tensor["Data"];
std::transform(dataVec.cbegin(), dataVec.cend(), dataPtr.get(), [](float data) { return data; });
}
Error -
cannot increment value of type 'std::unique_ptr<float[]>'
Question - How do I use std::transform in this case, where I dont have an iterator
英文:
I have a json array as shown below
{
{ "Data":[1.0, 2.0, 3.0] }
}
I am trying to copy this json array to a float array using std::transform
.
In code -
#include <nlohmann/json.hpp>
#include <memory>
using json = nlohmann::json;
int main(){
json tensor;
tensor["Data"] = { 1.0, 2.0, 3.0 };
auto dataPtr = std::make_unique<float[]>(3);
const auto& dataVec = tensor["Data"];
std::transform(dataVec.cbegin(), dataVec.cend(), dataPtr, [](float data) { return data; });
}
Error -
cannot increment value of type 'std::unique_ptr<float[]>'
Question - How do I use std::transform in this case, where I dont have an iterator
答案1
得分: 3
std::transform
可能无法直接增加unique_ptr
本身,但您可以获取其中包含的指针。原始指针是可递增的,因此可以用作转换的目标:
std::transform(dataVec.cbegin(), dataVec.cend(), dataPtr.get(), [](float data) { return data; });
英文:
std::transform
may not be able to increment the unique_ptr
itself, but you can get the contained pointer. The raw pointer is incrementable, so it is usable as a target for the transform:
std::transform(dataVec.cbegin(), dataVec.cend(), dataPtr.get(), [](float data) { return data; });
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论