用 C++ 比较两个重复字段 API
Compare two Repeated Fields with the C++ API
假设我有两个以下 protobuf 结构的实例:
message customStruct
{
optional int32 a = 1;
optional int32 b = 2;
}
message info
{
repeated customStruct cs = 1;
optional int32 x = 2;
optional double y = 3;
}
message root
{
optional info inf = 1;
}
I know I can compare Messages with the C++ API 但我想直接比较两个重复字段(customStruct
这里),为了简单和性能优化。
理想情况下,我需要 C# 方法的 C++ 等价物 Equals(RepeatedField< T > other)。
这在 C++ 中可行吗?这是一个好习惯吗?
RepeatedField<T>
has STL-like iterators, so you can use std::equal
比较它们:
#include <algorithm>
#include <...>
const google::protobuf::ReapeatedField<int32> & myField1 = ...;
const google::protobuf::ReapeatedField<int32> & myField2 = ...;
bool fieldsEqual = std::equal(myField1.begin(), myField1.end(), myField2.begin());
补充@jdehesa 的回答:
#include <algorithm>
#include <...>
const google::protobuf::ReapeatedField<int32> & myField1 = ...;
const google::protobuf::ReapeatedField<int32> & myField2 = ...;
bool fieldsEqual = std::equal(myField1.begin(), myField1.end(), myField2.begin(),
google::protobuf::utils::MessageDifferencer::Equals);
假设我有两个以下 protobuf 结构的实例:
message customStruct
{
optional int32 a = 1;
optional int32 b = 2;
}
message info
{
repeated customStruct cs = 1;
optional int32 x = 2;
optional double y = 3;
}
message root
{
optional info inf = 1;
}
I know I can compare Messages with the C++ API 但我想直接比较两个重复字段(customStruct
这里),为了简单和性能优化。
理想情况下,我需要 C# 方法的 C++ 等价物 Equals(RepeatedField< T > other)。
这在 C++ 中可行吗?这是一个好习惯吗?
RepeatedField<T>
has STL-like iterators, so you can use std::equal
比较它们:
#include <algorithm>
#include <...>
const google::protobuf::ReapeatedField<int32> & myField1 = ...;
const google::protobuf::ReapeatedField<int32> & myField2 = ...;
bool fieldsEqual = std::equal(myField1.begin(), myField1.end(), myField2.begin());
补充@jdehesa 的回答:
#include <algorithm>
#include <...>
const google::protobuf::ReapeatedField<int32> & myField1 = ...;
const google::protobuf::ReapeatedField<int32> & myField2 = ...;
bool fieldsEqual = std::equal(myField1.begin(), myField1.end(), myField2.begin(),
google::protobuf::utils::MessageDifferencer::Equals);