简体   繁体   English

C ++将类对象矢量向量保存到文件中

[英]C++ save vector of vector of vector of class object into file

I have a class in c++ like the following: 我有一个c ++类,如下所示:

class myCls
{
public:
   myCls();
   void setAngle(float angle);
   void setArr(unsigned char arr[64]);    
   unsigned char arr[64];
   double angle;
   int index;

   static float calcMean(const unsigned char arr[64]);
   static float sqrt7(float x);

};

Now in my main program I have a 3D vector of the class: 现在在我的主程序中,我有一个类的3D矢量:

vector<vector<vector< myCls > > > obj;

The size of the vector is also dynamically changed. 矢量的大小也是动态变化的。 My question is that how can I store the content of my vector into a file and retrieve it afterward? 我的问题是如何将我的矢量内容存储到文件中并在之后检索它?

I have tried many ways with no success.This is my try: 我尝试过很多方法都没有成功。这是我的尝试:

std::ofstream outFile;
outFile.open(fileName, ios::out);
for(int i=0;i<obj.size();i++)
    {
        outFile.write((const char *)(obj.data()),sizeof(vector<vector<myCls> >)*obj.size());
    }
outFile.close();

And for reading it: 阅读它:

vector<vector<vector<myCls>>> myObj;
id(inFile.is_open())
{
    inFile.read((char*)(myObj.data()),sizeof(vector<vector<myCls> >)*obj.size());
}

What I get is only runTime error. 我得到的只是runTime错误。

Can anyone help me in this issue please? 有人可以帮我解决这个问题吗?

If you don't care too much about performance, try boost::serialization . 如果您不太关心性能,请尝试boost :: serialization Since they've already implemented serialization functions for stl containers, you would only have to write the serialize function for a myCL , and everything else comes for free. 因为他们已经为stl容器实现了序列化函数,所以你只需要为myCL编写serialize函数,其他一切都是免费的。 Since your member variables are all public, you can do that intrusively or non-intrusively . 由于您的成员变量都是公开的,因此您可以侵入性地或非侵入式地执行此操作

Internally, a vector most usually consists of two numbers, representing the current length and the allocated length (capacity), as well as a pointer to the actual data. 在内部, vector通常由两个数字组成,表示当前长度和分配的长度(容量),以及指向实际数据的指针。 So the size of the “raw” object is fixed and approximately thrice the size of a pointer. 因此“原始”对象的大小是固定的,大约是指针大小的三倍。 This is what your code currently writes. 这是您的代码当前编写的内容。 The values the pointer points at won't be stored. 指针指向的值将不会被存储。 When you read things back, you're setting the pointer to something which in most cases won't even be allocated memory, thus the runtime error. 当你读回来时,你将指针设置为在大多数情况下甚至不会被分配内存的东西,因此运行时错误。

In general, it's a really bad idea to directly manipulate the memory of any class which provides constructors, destructors or assignment operators. 通常,直接操作提供构造函数,析构函数或赋值运算符的任何类的内存是一个非常糟糕的主意。 Your code writing to the private members of the vector would thoroughly confuse memory management, even if you took care to restore the pointed-at data as well. 写入vector的私有成员的代码将彻底混淆内存管理,即使您已经注意恢复指向的数据也是如此。 For this reason, you should only write simple ( POD ) data the way you did. 因此,您应该只按照您的方式编写简单( POD )数据。 Everything else should be customized to use custom code. 应该自定义其他所有内容以使用自定义代码。

In the case of a vector, you'd probably store the length first, and then write the elements one at a time. 在向量的情况下,您可能首先存储长度,然后一次写入一个元素。 For reading, you'd read the length, probably reserve memory accordingly, and then read elements one at a time. 对于读取,您将读取长度,可能相应地保留内存,然后一次读取一个元素。 The boost::serialization templates suggested by Voltron will probably save you the trouble of implementing all that. Voltron建议boost::serialization模板可能会为您省去实现所有这些的麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM