c++ - 在 C++ 中填充重复的自定义 Protocol Buffer 字段

标签 c++ protocol-buffers

如何在 C++ 中填充重复的自定义 Protocol Buffer 字段?

示例 Protocol Buffer :

package protocol;
import "enumerations.proto";
option optimize_for=SPEED;

message UserCommandProtocol {
  required uint64 utcTime=1;
  required uint64 playerId=2;
  optional int32 targetId=3;
  optional int32 number=4;

  message pair {
      required float first = 1;
      required float second = 2;
  }

  repeated uint64 bucketId=5 [packed=true]; 
  repeated pair points=6;
  repeated pair backupPoints=7;

  required COMMANDS command=8;
  optional Type type=9;
  optional Orientation orientation=10;
  optional COMMANDS_PRIORITY priority=11;
}

我只对填充点有疑问。在我的代码中,我有一个对象 std::list<std::pair<float,float>> p ,我想将这些值复制到 UserCommandProtocol points .

最佳答案

遍历您的配对列表,添加它们中的每一个。

UserCommandProtocol user_command;

// then, iterate over the list... for each element of the list do:

    std::list<std::pair<float,float>>::iterator it = ...; 

    pair* added_pair = user_command.add_points();
    added_pair->set_first(it->first);
    added_pair->set_second(it->second);

您可能需要阅读 Fields 部分(特别是 Protocol Buffers 文档的 Repeated Embedded Message Fields 小节)。

关于c++ - 在 C++ 中填充重复的自定义 Protocol Buffer 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763809/

相关文章:

serialization - 将 google protobuf 用于增量消息

java - Intellij中无法识别的 Protocol Buffer 导入

go - ProtoMessage 方法的目的是什么?

C++在for循环之前是否获取参数

c++ - 为什么我在 C++ 中得到 "nan"值?

c++ - 在 Qt 中获取 "Physical Memory currently used by current process"错误

javascript - 使用 Google Protocol Buffer 在 C++ 和 JavaScript 端点之间序列化/反序列化数据?

protocol-buffers - TakeLock 中的 protobuf-net 并发性能问题

c++ - QObject 自定义属性何时从样式表中指定的值初始化

C++ 为什么反转路径是非法的?