【问题标题】:Is there a find algorithm for Google Protocol Buffers (Protobuf)?Google Protocol Buffers (Protobuf) 是否有查找算法?
【发布时间】:2022-10-25 20:23:53
【问题描述】:
Google Protobuf 数组对象有查找算法吗?
例如,如果我有一个带有重复字段的 protobuf,例如
message MyProto {
repeated int32 intArray = 1;
}
如何在intArray 中找到特定的整数值?
有一个为重复字段生成的erase 方法,但find 似乎没有任何类似的方法。
【问题讨论】:
标签:
c++
algorithm
find
protocol-buffers
【解决方案1】:
虽然没有为 C++ protobuf 对象生成 find 成员函数,但 begin 和 end 之类的 stl 函数可用,这意味着可以使用来自 #include <algorithm> 的 std::find。
if(find(intarray.cbegin(), intarray.cend(), value) != intarray.cend())
{
// found
}