zoukankan      html  css  js  c++  java
  • protobuf--嵌套repeated get set

    package test_namespace;
    
    message ChildMsg {
        message GrandSonMsg {
            optional string grandson = 1;    
        }
    
        repeated GrandSonMsg grandson_msg = 1;
    }
    
    message FatherMsg {
        repeated ChildMsg child_msg = 1;
    }
    
    
    #include <iostream>
    #include "test.pb.h"
    
    using namespace std;
    
    int main()
    {
        // 方式2
        /*
        test_namespace::FatherMsg father_msg; 
        test_namespace::ChildMsg* ch;
        test_namespace::ChildMsg::GrandSonMsg* gs;
    
        ch = father_msg.add_child_msg();
    
        gs = ch->add_grandson_msg();
        gs->set_grandson("hello");
    
        gs = ch->add_grandson_msg();
        gs->set_grandson("world");
    
        */
    
        // 方式1(建议)
        test_namespace::FatherMsg father_msg; 
    
        test_namespace::ChildMsg child_msg;
        child_msg.add_grandson_msg()->set_grandson("hello");
        child_msg.add_grandson_msg()->set_grandson("world");
    
        father_msg.add_child_msg()->CopyFrom(child_msg);
    
    
        // output
        for (int i = 0; i < father_msg.child_msg_size(); i++)
        {
            for (int j = 0; j < father_msg.child_msg(i).grandson_msg_size(); j++)
            {
                cout << father_msg.child_msg(i).grandson_msg(j).grandson() << endl;
            }
        }
    
    
        return 0;
    }
  • 相关阅读:
    PAT 甲级1135. Is It A Red-Black Tree (30)
    AVL树模板
    定时器模板
    Listview模板
    Hash二次探测
    BFS小结
    STL之set篇
    完全二叉树-已知中序排序,输出广度排序
    BZOJ2037: [Sdoi2008]Sue的小球
    poj1157LITTLE SHOP OF FLOWERS
  • 原文地址:https://www.cnblogs.com/helloweworld/p/4211563.html
Copyright © 2011-2022 走看看