zoukankan      html  css  js  c++  java
  • proto3 中的 map 类型

    .proto

    syntax = "proto3";
    option optimize_for = SPEED;
    
    message TestStruct
    {
        map<int32,string> data = 1;
    }

    .cpp

    #include <iostream>
    #include <cstdio>
    #include <list>
    #include <string>
    #include <cstdint>
    #include <ctime>
    #include "msg.pb.h"
    #include "google/protobuf/text_format.h"
    using namespace google::protobuf;
    
    int32_t main()
    {
        TestStruct tSt1;
        tSt1.mutable_data()->insert({ 1, "str1" });
        tSt1.mutable_data()->insert({ 1, "str11" });
        tSt1.mutable_data()->insert(MapPair<int32_t, std::string>(2, "str2"));
        std::string data;
        tSt1.SerializeToString(&data);
    
        TestStruct tSt2;
        tSt2.ParseFromString(data);
        for(auto it = tSt2.data().cbegin(); it != tSt2.data().cend(); ++it)
        {
            std::cout << it->first << " " << it->second << std::endl;
        }
    
        std::string strTest;
        TextFormat::PrintToString(tSt2, &strTest);
        std::cout << strTest << std::endl;
    
        std::cin.get();
        return 0;
    }

    运行结果:

    注意:当再次插入重复的key的时候,插入操作将会失败.

  • 相关阅读:
    第五课 主引导程序的扩展 下
    C.Candy
    B.大钉骑马走江湖
    A喝酒(北京林业大学校赛)
    HDU 5666 Segment
    南京理工大学第八届校赛题目题解(部分)
    TCO 2016 Round 1B
    139. Word Break
    90. Subsets II
    78. Subsets
  • 原文地址:https://www.cnblogs.com/tangxin-blog/p/8314563.html
Copyright © 2011-2022 走看看