C++ Serializing a std::map to a file

后端 未结 6 802
星月不相逢
星月不相逢 2020-12-31 07:07

I have a C++ STL map, which is a map of int and customType. The customType is a struct, which has string and a list of string, How can i serialize this to a file.

sa

6条回答
  •  猫巷女王i
    2020-12-31 07:48

    A simple solution is to output each member on a line on its own, including all the strings in the list. Each record start with the key to the map, and ends with a special character or character sequence that can not be in the list. This way you can read one line at a time, and know the first line is the map key, the second line the first string in the structure and so on, and when you reach your special record-ending sequence you know the list is done and it's time for the next item in the map. This scheme makes the files generated readable, and editable if you need to edit them outside the program.

提交回复
热议问题