protobuf中repeated类型变量与C++ vector类型变量的相互赋值方法

您所在的位置:网站首页 SEO和SEM推广是什么 protobuf中repeated类型变量与C++ vector类型变量的相互赋值方法

protobuf中repeated类型变量与C++ vector类型变量的相互赋值方法

2023-09-09 11:45| 来源: 网络整理| 查看: 265

1.给repeated类型的变量赋值 1.1 逐一赋值

定义protobuf结构如下:

message Person { required int32 age = 1; required string name = 2; } message Family { repeated Person person = 1; }

对person进行赋值的方法如下:

int main(int argc, char* argv[]) { GOOGLE_PROTOBUF_VERIFY_VERSION; Family family; Person* person; // 添加一个家庭成员,John person = family.add_person(); person->set_age(25); person->set_name("John"); // 添加一个家庭成员,Lucy person = family.add_person(); person->set_age(23); person->set_name("Lucy"); // 添加一个家庭成员,Tony person = family.add_person(); person->set_age(2); person->set_name("Tony"); // 显示所有家庭成员 int size = family.person_size(); cout enum NavigationMode{ STATIC_DESTINATION = 0; DYNAMIC_DESTINATION = 1; PATH_PATROLLING = 2; } optional NavigationMode navigation_mode = 1;// 导航的模式 optional PointLLHA destination_point = 2;// 目标点 repeated VehicleMissionPoint mission_points = 3;// 途经点 optional double lateral_offset = 4;// 相对于目标的横向偏差 optional double longitudinal_offset = 5; // 相对于目标的纵向偏差 optional double speed_limit = 6;// 速度限制 }

对mission_points进行赋值的方法如下:

std::vector mission_points; VehicleNavigationStage navigation_stage; navigaion_stage.mutable_mission_points()->CopyFrom({mission_points.begin(), mission_points.end()}); 2.将repeated类型的变量赋值给vector类型

在C++中通常用vector来存repeated类型的变量,所以将repeated类性的变量赋值给vector类型的变量也是常见的用法。

定义protobuf结构如下:

message VehicleNavigationStage { enum NavigationMode{ STATIC_DESTINATION = 0; DYNAMIC_DESTINATION = 1; PATH_PATROLLING = 2; } optional NavigationMode navigation_mode = 1;// 导航的模式 optional PointLLHA destination_point = 2;// 目标点 repeated VehicleMissionPoint mission_points = 3;// 途经点 optional double lateral_offset = 4;// 相对于目标的横向偏差 optional double longitudinal_offset = 5; // 相对于目标的纵向偏差 optional double speed_limit = 6;// 速度限制 }

将mission_points赋值给vector型变量的方法如下:

std::vector mission_points; VehicleNavigationStage navigation_stage; for (int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3