serialization - C++ Serialize/Deserialize std::map<int,int> from/to file -


i have std::map.

i know if can write file (and read file) in 1 line using fwrite, or if need write/read each value separately.

i hoping since nothing special, might possible.

use boost::serialization serialize in 1 line. header it:

boost/serialization/map.hpp 

code example

#include <map> #include <sstream>  #include <boost/serialization/map.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp>  int main() {    std::map<int, int> map = {{1,2}, {2,1}};    std::stringstream ss;    boost::archive::text_oarchive oarch(ss);    oarch << map;    std::map<int, int> new_map;    boost::archive::text_iarchive iarch(ss);    iarch >> new_map;    std::cout << (map == new_map) << std::endl; } 

output:

g++ -o new new.cpp -std=c++0x -lboost_serialization ./new 1 

for file use std::ifstream/std::ofstream instead of std::stringstream , may binary_archive, instead of text_archive.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -