Jump to content
  • Advertisement
Sign in to follow this  
giugio

[c++]Serialize map

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!