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.
4 Answers
Yes, it's possible. With boost.serialization, for example.
For STL, read corresponding tutorial section

- 12,345
- 5
- 44
- 76
-
I was able to do with strings, can it be done directly with set for example? – user963241 Dec 12 '10 at 16:12
-
3@cpx: `#include
` – Abyx Dec 12 '10 at 16:40 -
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. – Elazar Leibovich Jun 27 '11 at 11:27
-
@Elazar Leibovich: there is no difference where to write `ar & str;` - inside a method, or inside a function. – Abyx Jun 27 '11 at 13:33
-
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). – Elazar Leibovich Jun 28 '11 at 06:01
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
- The
#include <boost/archive/text_iarchive.hpp>
must be before any otherboost
serialization includes. - You need to include a header for the STL type you want to archive.

- 32,750
- 33
- 122
- 169
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);

- 239,568
- 38
- 324
- 436
-
1Sure, if the strings contain newlines this won't work. I'd be willing to bet the OP's don't, though. – John Zwinck Jun 29 '11 at 01:57
-
Why won't you separate them with a null character instead? C strings usually don't contain those. – Chi-Lan Jun 29 '11 at 08:05
-
Because that would make the output file harder for humans to read. – John Zwinck Jun 30 '11 at 19:17
-
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. – John Zwinck Jul 05 '11 at 16:34
-
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. – Chi-Lan Jul 06 '11 at 10:16
-
1@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. – user2913685 Mar 25 '16 at 03:28
check this out . lite enough
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.