我如何将 c++ 中的向量序列化为 char,以便于将 mondodb 用于后端

How I can serialize vector in c++ to char for easy using mondodb for backend

本文关键字:mondodb 后端 用于 char 序列化 c++ 向量      更新时间:2023-10-16

我有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++领域工作以来已经有一段时间了,但向量本质上是一个数组,您可以使用 MongoDB 站点上的 BSON 文档生成器示例让 Cxx 驱动程序遍历数组并构造文档:

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

要获得对矢量的类似数组的访问,请执行以下操作:

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驱动程序的经验更加有限,但我希望您有足够的指针/开始继续您的工作。