gpt4 book ai didi

c++ - 我如何在 C++ 中将 vector 序列化为 char,以便轻松使用 mongodb 作为后端

转载 作者:行者123 更新时间:2023-11-28 04:49:38 24 4
gpt4 key购买 nike

我有 vector<matrix<float,o,1>> obj_id;

矩阵是:

class matrix : public matrix_exp<matrix<T,num_rows,num_cols, mem_manager,layout> > 

我想将每个项目写入 mongodb。我找不到转换解决方案。

但是我可以序列化每一项来写mongodb。但是当然大部分序列化到文件。

如何序列化为 char 或任何方便的变量以将 mongodb 作为二进制文件插入?

最佳

最佳答案

免责声明:自从我在 C++ 领域工作以来已经有一段时间了,但 vector 本质上是一个数组,您可以使用 MongoDB 站点上的 BSON Document Builder 示例让 Cxx 驱动程序遍历数组并构建文档:

https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/working-with-bson/

要获得对 vector 的类数组访问:

if(obj_id.size()) {
// create the pointer to the array
double *myarray = &obj_id[0];

// create a bson array builder and populate
auto array_builder = bsoncxx::builder::basic::array{};

for (const auto& element : elements) {
array_builder.append(element);
}

// Add the array_builder result into a document
// and save into the database
...
}

同样,我的 C++ 有点生疏,我对 MongoDB Cxx 驱动程序的经验更加有限,但我希望你有足够的指针/开始继续你的工作。

关于c++ - 我如何在 C++ 中将 vector 序列化为 char,以便轻松使用 mongodb 作为后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48499804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com