关于vector 序列化 的问题

precious_me 2014-09-29 11:44:42
各位大神,目前的情况是我自己定义了一个类,里面有多个数据成员,然后我定义了这个类的一个容器vector,现在想通过保存vector的数据下次启动程序时还原最后修改状态,可是不会操作vector,找了几天的资料了,求各位大神指教,ps:类的数据成员的类型是很多种的!
exmple
Class CMyTracker
{
UINT m_nStyle; // current state
CRect m_rect; // current position (always in pixels)
CSize m_sizeMin; // minimum X and Y size during track operation
int m_nHandleSize; // size of resize handles (default from WIN.INI)

//当前矩形边框颜色
COLORREF m_rectColor;
//当前矩形填充颜色
COLORREF m_bkColor;

BOOL m_bAllowInvert; // flag passed to Track or TrackRubberBand
CRect m_rectLast;
CSize m_sizeLast;
BOOL m_bErase; // TRUE if DrawTrackerRect is called for erasing
BOOL m_bFinalErase; // TRUE if DragTrackerRect called for final erase
CRect m_LimitRect; //限定tracker的活动区域
};
std::vector<CMyTracker> m_vTracker;
现在我想保存m_vTracker 中的数据成员。。感觉好无力啊!
也可以帮我提供点解决方案,我指目前是想到的方法是序列化,有没有更好的办法啊!!
...全文
370 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
precious_me 2014-10-20
  • 打赏
  • 举报
回复
引用 12 楼 passion_wu128 的回复:
[quote=引用 8 楼 precious_me 的回复:] [quote=引用 3 楼 passion_wu128 的回复:]
#include <fstream>
#include <vector>
#include <windows.h>
using namespace std;
class CMyTracker
{
	UINT m_nStyle;      // current state
	CRect m_rect;       // current position (always in pixels)
	CSize m_sizeMin;    // minimum X and Y size during track operation
	int m_nHandleSize;  // size of resize handles (default from WIN.INI)

	//当前矩形边框颜色
	COLORREF m_rectColor;
	//当前矩形填充颜色
	COLORREF m_bkColor;

	BOOL m_bAllowInvert;    // flag passed to Track or TrackRubberBand
	CRect m_rectLast;
	CSize m_sizeLast;
	BOOL m_bErase;          // TRUE if DrawTrackerRect is called for erasing
	BOOL m_bFinalErase;     // TRUE if DragTrackerRect called for final erase
	CRect m_LimitRect; //限定tracker的活动区域

	friend ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker);
	friend ifstream& operator>>(ifstream& ifs, CMyTracker& tracker);
};

typedef vector<CMyTracker> Containter;

ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker)
{
	//todo
	return ofs;
}

ifstream& operator>>(ifstream& ifs, CMyTracker& tracker)
{
	//todo
	return ifs;
}

ofstream& operator<<(ofstream& ofs, const Containter& vec)
{
	ofs << vec.size();
	for(Containter::const_iterator it = vec.begin(); it != vec.end(); ++it)
	{
		ofs << *it;
	}
	return ofs;
}

ifstream& operator>>(ifstream& ifs, Containter& vec)
{
	Containter::size_type size = 0;
	ifs >> size;
	if(!vec.empty())
	{
		vec.clear();
	}
	while(size--)
	{
		CMyTracker tracker;
		ifs >> tracker;
		vec.push_back(tracker);
	}
	return ifs;
}
剩下的自己去写吧
额,我刚刚接触C++不久,重载了这两个操作符函数是否就可以直接用文件流操作了?[/quote] Yes, u can do it like this. Containter cont; ofstream ofs("filename"); ofs << cont;[/quote] ths,problem solved!!!
precious_me 2014-10-20
  • 打赏
  • 举报
回复
引用 13 楼 mymtom 的回复:
自己定个格式即可呀,输入和输出配合就好

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

//#include <windows.h>
typedef string CRect;
typedef string CSize;
typedef string COLORREF;
typedef bool BOOL;
typedef unsigned int UINT;

class CMyTracker
{
public:
    UINT m_nStyle;      // current state
    CRect m_rect;       // current position (always in pixels)
    CSize m_sizeMin;    // minimum X and Y size during track operation
    int m_nHandleSize;  // size of resize handles (default from WIN.INI)
 
    //当前矩形边框颜色
    COLORREF m_rectColor;
    //当前矩形填充颜色
    COLORREF m_bkColor;
 
    BOOL m_bAllowInvert;    // flag passed to Track or TrackRubberBand
    CRect m_rectLast;
    CSize m_sizeLast;
    BOOL m_bErase;          // TRUE if DrawTrackerRect is called for erasing
    BOOL m_bFinalErase;     // TRUE if DragTrackerRect called for final erase
    CRect m_LimitRect; //限定tracker的活动区域
 
    friend ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker);
    friend ifstream& operator>>(ifstream& ifs, CMyTracker& tracker);
};
 
typedef vector<CMyTracker> Containter;
 
/*
template<typename T>
ofstream& operator<<(ofstream& ofs, const vector<T>& vec)
{
    ofs << vec.size() << endl;
    for(vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it)
    {
        ofs << *it;
    }
    return ofs;
}
*/

ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker)
{
    //todo
    ofs << tracker.m_nStyle << " , ";
    ofs << tracker.m_rect << " , ";

    // ...
    
    ofs << tracker.m_LimitRect << endl;
    
    return ofs;
}
 
ifstream& operator>>(ifstream& ifs, CMyTracker& tracker)
{
    //todo
    string s;

    ifs >> tracker.m_nStyle >> s;
    ifs >> tracker.m_rect >> s;
    ifs >> tracker.m_LimitRect;

    return ifs;
}
 
ofstream& operator<<(ofstream& ofs, const Containter& vec)
{
    ofs << vec.size() << endl;
    for(Containter::const_iterator it = vec.begin(); it != vec.end(); ++it)
    {
        ofs << *it;
    }
    return ofs;
}
 
ifstream& operator>>(ifstream& ifs, Containter& vec)
{
    string s;
    Containter::size_type size = 0;
    ifs >> size;
    if(!vec.empty())
    {
        vec.clear();
    }
    while(size--)
    {
        CMyTracker tracker;
        ifs >> tracker;
        vec.push_back(tracker);
    }
    return ifs;
}

int
main(int argc, char *argv[])
{
    Containter vec, vec2;
    CMyTracker trk;

    trk.m_nStyle = 1;
    trk.m_rect = "m_rect1";
    trk.m_LimitRect = "m_LimitRect1";
    vec.push_back(trk);

    trk.m_nStyle = 2;
    trk.m_rect = "m_rect2";
    trk.m_LimitRect = "m_LimitRect2";
    vec.push_back(trk);

    ofstream ofs("demo017.txt");
    ofs << vec;
    ofs.close();

    ifstream ifs("demo017.txt");
    ifs >> vec2;

    cout << "=============" << endl;

    cout << vec2.size() << endl;
    for (Containter::const_iterator it = vec2.begin(); it != vec2.end(); ++it) {
        cout << (*it).m_nStyle << endl;
        cout << (*it).m_rect << endl;
        cout << (*it).m_LimitRect << endl;
    }

    return 0;
}
感谢各位细心知道,fstream确实很强大、问题解决了
passion_wu128 2014-09-30
  • 打赏
  • 举报
回复
引用 8 楼 precious_me 的回复:
[quote=引用 3 楼 passion_wu128 的回复:]
#include <fstream>
#include <vector>
#include <windows.h>
using namespace std;
class CMyTracker
{
	UINT m_nStyle;      // current state
	CRect m_rect;       // current position (always in pixels)
	CSize m_sizeMin;    // minimum X and Y size during track operation
	int m_nHandleSize;  // size of resize handles (default from WIN.INI)

	//当前矩形边框颜色
	COLORREF m_rectColor;
	//当前矩形填充颜色
	COLORREF m_bkColor;

	BOOL m_bAllowInvert;    // flag passed to Track or TrackRubberBand
	CRect m_rectLast;
	CSize m_sizeLast;
	BOOL m_bErase;          // TRUE if DrawTrackerRect is called for erasing
	BOOL m_bFinalErase;     // TRUE if DragTrackerRect called for final erase
	CRect m_LimitRect; //限定tracker的活动区域

	friend ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker);
	friend ifstream& operator>>(ifstream& ifs, CMyTracker& tracker);
};

typedef vector<CMyTracker> Containter;

ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker)
{
	//todo
	return ofs;
}

ifstream& operator>>(ifstream& ifs, CMyTracker& tracker)
{
	//todo
	return ifs;
}

ofstream& operator<<(ofstream& ofs, const Containter& vec)
{
	ofs << vec.size();
	for(Containter::const_iterator it = vec.begin(); it != vec.end(); ++it)
	{
		ofs << *it;
	}
	return ofs;
}

ifstream& operator>>(ifstream& ifs, Containter& vec)
{
	Containter::size_type size = 0;
	ifs >> size;
	if(!vec.empty())
	{
		vec.clear();
	}
	while(size--)
	{
		CMyTracker tracker;
		ifs >> tracker;
		vec.push_back(tracker);
	}
	return ifs;
}
剩下的自己去写吧
额,我刚刚接触C++不久,重载了这两个操作符函数是否就可以直接用文件流操作了?[/quote] Yes, u can do it like this. Containter cont; ofstream ofs("filename"); ofs << cont;
mymtom 2014-09-30
  • 打赏
  • 举报
回复
自己定个格式即可呀,输入和输出配合就好

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

//#include <windows.h>
typedef string CRect;
typedef string CSize;
typedef string COLORREF;
typedef bool BOOL;
typedef unsigned int UINT;

class CMyTracker
{
public:
    UINT m_nStyle;      // current state
    CRect m_rect;       // current position (always in pixels)
    CSize m_sizeMin;    // minimum X and Y size during track operation
    int m_nHandleSize;  // size of resize handles (default from WIN.INI)
 
    //当前矩形边框颜色
    COLORREF m_rectColor;
    //当前矩形填充颜色
    COLORREF m_bkColor;
 
    BOOL m_bAllowInvert;    // flag passed to Track or TrackRubberBand
    CRect m_rectLast;
    CSize m_sizeLast;
    BOOL m_bErase;          // TRUE if DrawTrackerRect is called for erasing
    BOOL m_bFinalErase;     // TRUE if DragTrackerRect called for final erase
    CRect m_LimitRect; //限定tracker的活动区域
 
    friend ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker);
    friend ifstream& operator>>(ifstream& ifs, CMyTracker& tracker);
};
 
typedef vector<CMyTracker> Containter;
 
/*
template<typename T>
ofstream& operator<<(ofstream& ofs, const vector<T>& vec)
{
    ofs << vec.size() << endl;
    for(vector<T>::const_iterator it = vec.begin(); it != vec.end(); ++it)
    {
        ofs << *it;
    }
    return ofs;
}
*/

ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker)
{
    //todo
    ofs << tracker.m_nStyle << " , ";
    ofs << tracker.m_rect << " , ";

    // ...
    
    ofs << tracker.m_LimitRect << endl;
    
    return ofs;
}
 
ifstream& operator>>(ifstream& ifs, CMyTracker& tracker)
{
    //todo
    string s;

    ifs >> tracker.m_nStyle >> s;
    ifs >> tracker.m_rect >> s;
    ifs >> tracker.m_LimitRect;

    return ifs;
}
 
ofstream& operator<<(ofstream& ofs, const Containter& vec)
{
    ofs << vec.size() << endl;
    for(Containter::const_iterator it = vec.begin(); it != vec.end(); ++it)
    {
        ofs << *it;
    }
    return ofs;
}
 
ifstream& operator>>(ifstream& ifs, Containter& vec)
{
    string s;
    Containter::size_type size = 0;
    ifs >> size;
    if(!vec.empty())
    {
        vec.clear();
    }
    while(size--)
    {
        CMyTracker tracker;
        ifs >> tracker;
        vec.push_back(tracker);
    }
    return ifs;
}

int
main(int argc, char *argv[])
{
    Containter vec, vec2;
    CMyTracker trk;

    trk.m_nStyle = 1;
    trk.m_rect = "m_rect1";
    trk.m_LimitRect = "m_LimitRect1";
    vec.push_back(trk);

    trk.m_nStyle = 2;
    trk.m_rect = "m_rect2";
    trk.m_LimitRect = "m_LimitRect2";
    vec.push_back(trk);

    ofstream ofs("demo017.txt");
    ofs << vec;
    ofs.close();

    ifstream ifs("demo017.txt");
    ifs >> vec2;

    cout << "=============" << endl;

    cout << vec2.size() << endl;
    for (Containter::const_iterator it = vec2.begin(); it != vec2.end(); ++it) {
        cout << (*it).m_nStyle << endl;
        cout << (*it).m_rect << endl;
        cout << (*it).m_LimitRect << endl;
    }

    return 0;
}
幻夢之葉 2014-09-29
  • 打赏
  • 举报
回复
格式化写入一个文件中,启动的时候检测并且读入数据就好了。
precious_me 2014-09-29
  • 打赏
  • 举报
回复
自己先顶顶!!!!!!
排山和倒海 2014-09-29
  • 打赏
  • 举报
回复
其实就相当于自己实现一个python的cPickle 也就是将类写入文件,然后读出就可以了。
precious_me 2014-09-29
  • 打赏
  • 举报
回复
引用 5 楼 cjzzmdn 的回复:
[quote=引用 4 楼 lovesmiles 的回复:] 如果类中没有虚函数,直接将一个类当结构体,将内存按字节写入文件。 读时按字节读入内存,将指向这部分内存的指针转换成类指针,即可使用。
或者参考webservice 存成xml格式的 json格式的[/quote] 恩,同事也说过可以考虑,但是没接触过,不知道难不难。。
precious_me 2014-09-29
  • 打赏
  • 举报
回复
引用 4 楼 lovesmiles 的回复:
如果类中没有虚函数,直接将一个类当结构体,将内存按字节写入文件。 读时按字节读入内存,将指向这部分内存的指针转换成类指针,即可使用。
虚函数是有的,我只是举了个例子,类里面的成员比较的。但是好像没有涉及到复杂的数据结构,都是些位置的坐标,大小等数据。
precious_me 2014-09-29
  • 打赏
  • 举报
回复
引用 3 楼 passion_wu128 的回复:
#include <fstream>
#include <vector>
#include <windows.h>
using namespace std;
class CMyTracker
{
	UINT m_nStyle;      // current state
	CRect m_rect;       // current position (always in pixels)
	CSize m_sizeMin;    // minimum X and Y size during track operation
	int m_nHandleSize;  // size of resize handles (default from WIN.INI)

	//当前矩形边框颜色
	COLORREF m_rectColor;
	//当前矩形填充颜色
	COLORREF m_bkColor;

	BOOL m_bAllowInvert;    // flag passed to Track or TrackRubberBand
	CRect m_rectLast;
	CSize m_sizeLast;
	BOOL m_bErase;          // TRUE if DrawTrackerRect is called for erasing
	BOOL m_bFinalErase;     // TRUE if DragTrackerRect called for final erase
	CRect m_LimitRect; //限定tracker的活动区域

	friend ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker);
	friend ifstream& operator>>(ifstream& ifs, CMyTracker& tracker);
};

typedef vector<CMyTracker> Containter;

ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker)
{
	//todo
	return ofs;
}

ifstream& operator>>(ifstream& ifs, CMyTracker& tracker)
{
	//todo
	return ifs;
}

ofstream& operator<<(ofstream& ofs, const Containter& vec)
{
	ofs << vec.size();
	for(Containter::const_iterator it = vec.begin(); it != vec.end(); ++it)
	{
		ofs << *it;
	}
	return ofs;
}

ifstream& operator>>(ifstream& ifs, Containter& vec)
{
	Containter::size_type size = 0;
	ifs >> size;
	if(!vec.empty())
	{
		vec.clear();
	}
	while(size--)
	{
		CMyTracker tracker;
		ifs >> tracker;
		vec.push_back(tracker);
	}
	return ifs;
}
剩下的自己去写吧
额,我刚刚接触C++不久,重载了这两个操作符函数是否就可以直接用文件流操作了?
超级能量泡泡 2014-09-29
  • 打赏
  • 举报
回复
不过需要注意struct alignment必须约定好,否则可能改了以后导入不了以前的数据
超级能量泡泡 2014-09-29
  • 打赏
  • 举报
回复
你这种类看上去是POD的 也就是不包含复杂指针成员的纯线性存储 处理方法应该和vector<int>是一样的
cjzzmdn 2014-09-29
  • 打赏
  • 举报
回复
引用 4 楼 lovesmiles 的回复:
如果类中没有虚函数,直接将一个类当结构体,将内存按字节写入文件。 读时按字节读入内存,将指向这部分内存的指针转换成类指针,即可使用。
或者参考webservice 存成xml格式的 json格式的
勤奋的小游侠 2014-09-29
  • 打赏
  • 举报
回复
如果类中没有虚函数,直接将一个类当结构体,将内存按字节写入文件。 读时按字节读入内存,将指向这部分内存的指针转换成类指针,即可使用。
passion_wu128 2014-09-29
  • 打赏
  • 举报
回复
#include <fstream>
#include <vector>
#include <windows.h>
using namespace std;
class CMyTracker
{
	UINT m_nStyle;      // current state
	CRect m_rect;       // current position (always in pixels)
	CSize m_sizeMin;    // minimum X and Y size during track operation
	int m_nHandleSize;  // size of resize handles (default from WIN.INI)

	//当前矩形边框颜色
	COLORREF m_rectColor;
	//当前矩形填充颜色
	COLORREF m_bkColor;

	BOOL m_bAllowInvert;    // flag passed to Track or TrackRubberBand
	CRect m_rectLast;
	CSize m_sizeLast;
	BOOL m_bErase;          // TRUE if DrawTrackerRect is called for erasing
	BOOL m_bFinalErase;     // TRUE if DragTrackerRect called for final erase
	CRect m_LimitRect; //限定tracker的活动区域

	friend ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker);
	friend ifstream& operator>>(ifstream& ifs, CMyTracker& tracker);
};

typedef vector<CMyTracker> Containter;

ofstream& operator<<(ofstream& ofs, const CMyTracker& tracker)
{
	//todo
	return ofs;
}

ifstream& operator>>(ifstream& ifs, CMyTracker& tracker)
{
	//todo
	return ifs;
}

ofstream& operator<<(ofstream& ofs, const Containter& vec)
{
	ofs << vec.size();
	for(Containter::const_iterator it = vec.begin(); it != vec.end(); ++it)
	{
		ofs << *it;
	}
	return ofs;
}

ifstream& operator>>(ifstream& ifs, Containter& vec)
{
	Containter::size_type size = 0;
	ifs >> size;
	if(!vec.empty())
	{
		vec.clear();
	}
	while(size--)
	{
		CMyTracker tracker;
		ifs >> tracker;
		vec.push_back(tracker);
	}
	return ifs;
}
剩下的自己去写吧

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧