Serialization of STL Class

15,485

Solution 1

Yes, it's possible. With boost.serialization, for example.

For STL, read corresponding tutorial section

Solution 2

An example of using boost::serialization to serialize an STL type

#include <map>
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/map.hpp>

int main(int argc,char** argv) {
  std::ofstream s("tmp.oarchive");
  boost::archive::text_oarchive oa(s);
  std::map<int,int> m;
  m[1] = 100;
  m[2] = 200;
  oa << m;
}

Compile with

g++ -lboost_serialization myfile.cc

Note that

  1. The #include <boost/archive/text_iarchive.hpp> must be before any other boost serialization includes.
  2. You need to include a header for the STL type you want to archive.

Solution 3

If you just want to write a std::set<std::string> to a file and read it back out, and your project doesn't already use Boost, you might try something simple:

ofstream file("file.txt");
copy(theSet.begin, theSet.end(), ostream_iterator<string>(file, "\n"));

This will simply write the strings, one per line, into a text file. Then to read them:

ifstream file("file.txt");
string line;
while(getline(file, line))
    theSet.insert(line);

Solution 4

check this out . lite enough

STL serialization

ONLY ONE CPP FILE NEEDED

A lite serialization solution

there are several lib out there support serialization,like protobuffer, boost:serialization , too heavy for me. so I wrote this lite version.

support

  • vector
  • map
  • set
  • string
  • primitives(int,double,long,...)
  • endian support
  • nesting container support

use char instead bool in STL why

build

you can build this project by CMake. or just import serialization.h into your project.

define CHECK_ENDIAN=1 if you wanna check endian

demo

check testSerialization.cpp

comming soom

endian conversion.

Share:
15,485
user963241
Author by

user963241

Updated on June 25, 2022

Comments

  • user963241
    user963241 almost 2 years

    is it possible serialize any STL class including std::string? I've a sets of std::strings and I'm trying to write them into file and load them back into std::set.

  • user963241
    user963241 over 13 years
    I was able to do with strings, can it be done directly with set for example?
  • Abyx
    Abyx over 13 years
    @cpx: #include <boost/serialization/set.hpp>
  • Elazar Leibovich
    Elazar Leibovich almost 13 years
    He don't want an example that makes a class with an STL container, he wants an example of a function that gets an STL container and tucks it to a file.
  • Abyx
    Abyx almost 13 years
    @Elazar Leibovich: there is no difference where to write ar & str; - inside a method, or inside a function.
  • Elazar Leibovich
    Elazar Leibovich almost 13 years
    maybe one can change the example to work as the user requested, but currently it doesn't. (What class is expected to be in Archive? How do you use it? The example show the serialize method which is used implicitly by the text_oarchive, there's no example how the user would do that).
  • John Zwinck
    John Zwinck almost 13 years
    Sure, if the strings contain newlines this won't work. I'd be willing to bet the OP's don't, though.
  • Chi-Lan
    Chi-Lan almost 13 years
    Why won't you separate them with a null character instead? C strings usually don't contain those.
  • John Zwinck
    John Zwinck almost 13 years
    Because that would make the output file harder for humans to read.
  • John Zwinck
    John Zwinck almost 13 years
    Since when is obscurity good? It's nice if users can edit the files written out before loading them again. And they shouldn't have to know about some magical "obscure unicode line break" to do it. Obscure bad. Simple good.
  • Chi-Lan
    Chi-Lan almost 13 years
    Unless simple breaks, like here... At least you should throw an exception if there are newlines in the string, and not just accept them and emit serialized havoc.
  • user2913685
    user2913685 about 8 years
    @JohnZwinck to stop the problem with 0xA aka '\n' you can use another number from 0x1 - 0x1F that gives you the functionality you'd like. For example: 0x1E which is a the record separator character.