hashmap - c++ hash_map 持久化
高洛峰
高洛峰 2017-04-17 11:59:51
0
3
667

c++ 中怎么将hash_map 存储到文件中 这个牵扯到序列化和反序列化 有做过这方面的大神吗?据说boost 库提供了一个 方法 但是查到的资料很少。有谁有这方面的资料 共享下 谢谢!!!

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
Peter_Zhu

As long as hash_map can be traversed, it can be serialized. Generally, collection data structures can be traversed, and hash_map is no exception

Serialization

Get the number of items through the size() method of the hash_map object and write it to the stream,
Traverse the hash_map object through hash_map::iterator and write the key and value to the stream in turn.

Deserialization

A hash_map object is generated when reading,
First read the quantity, and then perform a for loop based on this quantity,
Read the key and value pairs in sequence and insert them into the hash_map object just now

左手右手慢动作

If you consider a third-party library, Google’s protobuf is recommended, which can directly serialize the hash_map object and store it;

大家讲道理

I don’t know the size of your data and the modification frequency of hash_map. Let me just talk about my current situation and implementation
My map has about a few thousand to tens of thousands items. Additions, deletions and modifications are infrequent, and persistence is done in shared memory.
Therefore, the shared memory structure defines a struct, which contains key and value. When the program is running, all additions, deletions, and modifications to the map are converted into writes to the shared memory. When the program restarts, all items are read from the shared memory and the map is re-formed.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!