[c++]Serialize map

Started by
0 comments, last by giugio 15 years, 3 months ago
Hy. I'm trying to serialize a std::map with boost: the problem is that the boost library serialize the map,but when i unserialize for load the data from the archive in void openDocument(map<std::string,double> *vect,const string &filename) { std::ifstream ifs(filename.c_str(), std::ios_base::binary); boost::archive::binary_iarchive ia(ifs); ia >> * vect;//vect:the first item is correct , but the othe no!!! ifs.close(); } the first item of the map is correct , but the other are not correct std::map<std::string,double,hashfnv::fnv_1>dictionary; this is the code:

void saveDocument(map<std::string,double> doc, const string &filename) {
  ofstream ofs(filename.c_str());
  boost::archive::binary_oarchive oa(ofs);
  oa << doc;
  ofs.close();
}

void openDocument(map<std::string,double> *vect,const string &filename) {
  
  std::ifstream ifs(filename.c_str(), std::ios_base::binary);
  boost::archive::binary_iarchive ia(ifs);
  ia >> * vect;
  ifs.close();
}

vector<vector<PCI>>m_vAll;
vector<PCI>m_v;
int main()
{
	for(int i = 0;i<5;i++)
	{
		PCI pci;
		pci.x = 1;
		pci.y = 2;
		pci.z = 3;
		m_v.push_back(pci);
	}
	m_vAll.push_back(m_v);
	m_v.clear();
	for(int i = 0;i<5;i++)
	{
		PCI pci;
		pci.x = 4;
		pci.y = 5;
		pci.z = 6;
		m_v.push_back(pci);
	}
	m_vAll.push_back(m_v);
	m_v.clear();

	std::map<std::string,double>dictionary;

	
	dictionary["a"]=1.23;
	dictionary["b"]=2.23;
	
	saveDocument(dictionary,"c:\\giugio.bin");
	dictionary.clear();
	openDocument(&dictionary,"c:\\giugio.bin");
Thanks.
Advertisement
solved:
i change binary_oarchive to text_oarchive and binary_iarchive to text_iarchive and all work.
How to correct the binary archive for serialize/deserialize?Thanks.
Because i would use binary serializer for prestation term

This topic is closed to new replies.

Advertisement