Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7529465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:40:05+00:00 2026-05-30T04:40:05+00:00

I want to serialize a hash_multimap, does protocal buffers support it ? I have

  • 0

I want to serialize a hash_multimap, does protocal buffers support it ? I have tried boost serializaitn but it has header file confusion about hash_multimap so I want to try google protocol buffers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:40:06+00:00Added an answer on May 30, 2026 at 4:40 am

    There’s a fair bit of boilerplate coding that goes with using protocol buffers, and you need to run their protoc compiler to generate the actual C++ from the .proto files, but aside from that they’re great.

    Here’s an example of how you’d serialise and parse a std::hash_multimap

    my_hash_multimap.proto

    message MyHashMultimap {
      message Pair {
        required int64 key = 1;
        required bytes value = 2;
      }
      repeated Pair pair = 1;
    }
    

    main.cpp

    #include <algorithm>
    #include <hash_map>
    #include <iostream>
    #include <string>
    #include <utility>
    
    #include "my_hash_multimap.pb.h"
    
    int main() {
      std::hash_multimap<int, std::string> hm;
      hm.insert(std::make_pair(3, "three"));
      hm.insert(std::make_pair(2, "two"));
      hm.insert(std::make_pair(1, "one"));
    
      // convert std::hash_multimap to a protobuf MyHashMultimap
      MyHashMultimap proto_hm;
      std::for_each(hm.begin(),
                    hm.end(),
                    [&proto_hm](std::pair<int, std::string> p) {
        std::cout << p.first << "   " << p.second << std::endl;
        // add new Pair to proto_hm
        MyHashMultimap::Pair* proto_pair(proto_hm.add_pair());
        // set this Pair's values
        proto_pair->set_key(p.first);
        proto_pair->set_value(p.second);
      });
    
      // serialise proto_hm to a std::string
      std::string serialised_hm(proto_hm.SerializeAsString());
    
      // parse from this string to a new MyHashMultimap
      MyHashMultimap parsed_proto_hm;
      if (!parsed_proto_hm.ParseFromString(serialised_hm))
        return -1;
      std::cout << std::endl << parsed_proto_hm.DebugString() << std::endl << std::endl;
    
      // convert protobuf MyHashMultimap to a std::hash_multimap
      std::hash_multimap<int, std::string> parsed_hm;
      for (int i(0); i != parsed_proto_hm.pair_size(); ++i) {
        // check required fields are populated correctly
        if (parsed_proto_hm.pair(i).IsInitialized()) {
          // add the Pair to parsed_hm
          parsed_hm.insert(std::make_pair(parsed_proto_hm.pair(i).key(),
                                          parsed_proto_hm.pair(i).value()));
        }
      }
    
      std::for_each(parsed_hm.begin(),
                    parsed_hm.end(),
                    [](std::pair<int, std::string> p) {
        std::cout << p.first << "   " << p.second << std::endl;
      });
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to Serialize a 60mb file into XML but it gives me System
I want serialize object to binary file using operator <<, but when I serialize,
I want to serialize my enum-value as an int, but i only get the
I have a collection of classes that I want to serialize out to an
in a C program I have an long* that I want to serialize (thus
I have a class that I want to serialize [System.Xml.Serialization.XmlTypeAttribute(Namespace = http://xyz.com/schema)] public class
I have a class that I want to serialize to xml. The class looks
I have an entity framework entity that i want to serialize as a json
Hi I have a Java enum and I want serialize a variable containing an
I want to serialize a collection of objects for storage/retrieval from my app.config file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.