How to convert a tabular format or python dataframe-equivalent format to msgpack format in C++

感情迁移 提交于 2019-12-24 19:27:09

问题


(Please Note: I am not able to embed images here. I dont have enough points for that. Could anyone please help me with that.)

I understand how to convert the struct corresponding to the following tablular format (Struct1) into msgpack format:

Struct1

For this I use the following code:

#include <sstream>
#include <iostream>
#include <msgpack.hpp>

inline std::ostream& hex_dump(std::ostream& o, std::string const& v) {
    std::ios::fmtflags f(o.flags());
    o << std::hex;
    o << "b\'";
    for (auto c : v) {
        o << "\\x" << std::setw(2) << std::setfill('0') << (static_cast<int>(c) & 0xff);
    }
    o << "\'";
    o.flags(f);
    return o;
}

struct your_type {
    int a;
    int b;
    MSGPACK_DEFINE(a, b);
};

int main() {
    // packing
    std::stringstream ss;
    std::stringstream sshex;
    std::string ssnew;
    std::vector<std::map<std::string, your_type>> v
    {
        {
            { "t",{ 1, 2} }

        },
        //{
            { "value",{6, 5 } }
        }
    };

    msgpack::pack(ss, v);


    auto oh = msgpack::unpack(ss.str().data(), ss.str().size());
    // JSON output
    std::cout << oh.get() << std::endl;

    std::cout << std::endl;
    // hex dump output
    hex_dump(sshex, ss.str()) << std::endl;

    std::cout << sshex.str();

    ssnew = sshex.str();

    return ssnew;

}

Now I would want to try the following to be converted in the same format:

I want to add each row in a loop, pass the values through the loop to the structure and then convert into msgpack format. Again, repeat the process after adding another row. I do not want to statically define the values as before. For eg:

First time - data passed for conversion (Struct2):

Struct2

Second time - data passed for conversion (Struct3): Struct3

I have tried with using int for t and int array for val. I am not able to proceed further. I have researched msgpack library but in vain. Could anyone please help me as how can I proceed with this? Even a little guidance would be a lot helpful.

来源:https://stackoverflow.com/questions/53544552/how-to-convert-a-tabular-format-or-python-dataframe-equivalent-format-to-msgpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!