[英]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;
}
我知道我可以將 Messages 與 C++ API 進行比較,但我想直接比較兩個重復字段(這里是customStruct
),為了簡單和性能優化。
理想情況下,我需要一個等效於 C# 方法Equals(RepeatedField< T > other)的 C++ 。
這在 C++ 中可行嗎? 這是一個好習慣嗎?
RepeatedField<T>
具有類似 STL 的迭代器,因此您可以使用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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.