c++ - 迭代时在 std::vector 中保存指向不同类型的指针
问题描述
考虑以下函数 (myfct)。当此函数返回时,数据指向 uint16_t、float_t 或 double_t。
在 main() 中,这个函数在 for 循环中被调用。我希望每次迭代都使用它指向的类型将“数据”保存在 std::vector 中。我怎样才能做到这一点?提前致谢。
PS:容器不一定是向量,可以是其他任何东西,我只是想在不丢失指针类型信息的情况下保存“数据”。
void myfct(void*&data, int id) {
if(id==1) {
uint16_t* ptr = ...;
data = ptr;
}
else if(id==2) {
float_t* ptr = ...;
data = ptr;
}
else if(id==3) {
double_t* ptr = ...;
data = ptr;
}
}
int main()
{
void* dataPtr = nullptr;
vector<?> myVec; //how should I template my vector? which typename?
for(int i = 0, i<5, i++)
{
myfct(dataPtr , i); //note that dataPtr is passed by reference
myVec.push_back(dataPtr ); //here dataPtr point to either uint16_t,
//float_t or double_t. I want to save it with the type it point to.
}
}
解决方案
如果您无权访问std::variant
,则方法如下:
class custom_type {
int id;
union data_holder
{
uint16_t i;
float_t f;
double_t d;
} data;
}
现在,您可以拥有一个vector<custom_type>
,如果您想使用数据点,您可以这样做
custom_type c = myvec.back();
if( c.id == 1 ) {
uint16_t val = c.data.i;
// Process val in a uint way
} else if( c.id == 2 ) {
float_t val = c.data.f;
// Process val in a float way
} ...
您可以在此处了解有关工会的更多信息。现在,为了让您的代码轻松处理数据类型(因此您不必一遍又一遍地编写类似的“以 X 方式处理 val”),您可以使用templates。例如,
template <typename T>
T calculate(T val) {
// Do stuff with val
return computed_val;
}
现在,
custom_type c = myvec.back();
if( c.id == 1 ) {
uint16_t val = calculate<uint16_t>(c.data.i);
cout << val;
} else if( c.id == 2 ) {
float_t val = calculate<float_t>(c.data.f);
cout << val;
} ...
推荐阅读
- javascript - Javascript - 如果对象的所有属性都为空,则将其设置为空
- python - 在 python 3.7 中导入 opencv 版本 3.4.2.17
- react-admin - 如何在 SimpleForm 上使用 asyncValidation 显示错误消息?
- javascript - 从 java 脚本调用 php 文件删除函数
- java - 从 CSV 文件中读取 NULL 值
- angular - Angular HttpClient 流式获取请求或有后端触发请求?
- facebook-graph-api - 通过 Android SDK 从 Facebook Graph API 获取公共事件
- three.js - Three.js Quaternion.slerp() 比 Vector3.lerpVectors() 慢
- vb.net - 将文件下载到应用程序路径
- javascript - XMLHttpRequest 到 JSON 文件不会返回所有后代对象,只有 32