Aar*_*Cee 9 c++ protocol-buffers
协议缓冲区库中是否有一个实现允许对指定为重复字段的数组进行排序?例如,假设数组由一个类型的项组成,该类本身包含一个索引字段,数据项需要根据该字段进行排序.我找不到它,所以我想我必须自己写一个.只是想确认一下.谢谢.
小智 14
Protobufs通过mutable_*方法提供RepeatedPtr接口,可以使用std :: sort()模板进行排序.
除非重复字段的基础类型是简单的,否则您可能希望使用重载的运算符<,比较器或lambda来执行此操作.使用lambda的玩具示例是:
message StaffMember {
    optional string name = 1;
    optional double hourly_rate = 2;
}
message StoreData {
    repeated StaffMember staff = 1;
}
StoreData store;
// Reorder the list of staff by pay scale
std::sort(store->mutable_staff()->begin(),
          store->mutable_staff()->end(),
          [](const StaffMember& a, const StaffMember& b){
             return a.hourly_rate() < b.hourly_rate();
          });