c++中的set自定义数据类型

#include<iostream>  
#include<stack>
#include<string>
#include<cmath>
#include<map>
#include<algorithm> 
#include<sstream>
#include<set>
#include<queue>
using namespace std;
struct all{
 int a;
 string str;
 bool operator<(const all& b) const//find和count均支持,const关键字必不可少
 {
  return str<b.str;
  } 
};
//struct cmp{//仅支持find,不支持count
// bool operator()(const all& a,const all& b)
// {
//  return a.str<b.str;
// }
//};
int main()
{
 
    set<all> my_set; 
    all a = {6,"zyh"};
 my_set.insert(a); 
 all b = {2,"ayh"};
 my_set.insert(b);
 all c= {4,"qwyh"};
 my_set.insert(c);
 if(my_set.find(a)==my_set.end())
  cout<<"can not find"<<endl;
 else
  cout<<"find"<<endl;
 if(my_set.count(a)==0)
  cout<<"can not find"<<endl;
 else
  cout<<"find"<<endl;
 for(set<all>::iterator it= my_set.begin();it!=my_set.end();it++)//模板类型不可少
  cout<<(*it ).str<<endl;
 return 0 ;
}

在 Apollo ,可以使用 protobuf 进行自定义数据类型的定义,并使用 RecordWriter 将数据写入 Record 。以下是一个简单的示例: 1. 定义 protobuf 文件 假设我们要定义一个自定义数据类型,包含一个字符串和一个整数。首先,我们需要在一个 .proto 文件定义这个数据类型: ``` syntax = "proto3"; package my_package; message MyCustomType { string str = 1; int32 num = 2; } ``` 2. 编译 protobuf 文件 我们需要使用 protoc 编译器将 .proto 文件编译成 C++ 代码。假设我们的 .proto 文件名为 my_custom_type.proto,我们可以使用以下命令进行编译: ``` protoc --cpp_out=. my_custom_type.proto ``` 这将生成一个名为 my_custom_type.pb.h 的头文件和一个名为 my_custom_type.pb.cc 的源文件。 3. 写入 Record 现在我们可以在 C++ 代码使用 MyCustomType 类型,并将它们写入 Record 。以下是一个示例: ```c++ #include <iostream> #include <fstream> #include "cyber/cyber.h" #include "my_package/my_custom_type.pb.h" using apollo::cyber::RecordWriter; using my_package::MyCustomType; int main() { // 初始化 Cyber apollo::cyber::Init("writer"); // 创建 RecordWriter RecordWriter writer("./record_test.rec"); // 创建 MyCustomType 对象 MyCustomType custom_type; custom_type.set_str("hello"); custom_type.set_num(123); // 将 MyCustomType 对象写入 Record writer.WriteChannel("channel_name", &custom_type); // 关闭 RecordWriter writer.Close(); return 0; } ``` 这个示例创建了一个 MyCustomType 对象,并将它写入一个名为 channel_name 的 channel 。然后,它关闭了 RecordWriter。 注意,在使用 RecordWriter 之前,我们需要初始化 Cyber。此外,我们还需要在 CMakeLists.txt 文件添加以下行: ``` add_subdirectory(my_package) ``` 这样可以将编译后的 my_custom_type.pb.h 和 my_custom_type.pb.cc 文件添加到项目。 以上就是使用 Apollo 自定义数据类型并写入 Record 的基本方法。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值