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

标签 c++ mongodb serialization

我有 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/

相关文章:

c# - Xml 序列化 : Serialize a Property as Attribute of another Property

C#反射字典

c++ - 添加到集合 STL - C++

c++ - 在类中调用函数

C++ 虚函数被隐藏

c++ - 在分配之前 memset() 一个 std::iterator 变量是否安全?

ruby - MongoMapper:如何创建这样的模型

javascript - NodeJS Passport 本地身份验证不起作用

Java MongoDB Morphia : find a object by ObjectId id

java - 将 Java 3d 对象写入自定义文件类型