首页 > 解决方案 > 迭代时在 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.
    }
}

标签: c++c++11pointers

解决方案


如果您无权访问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;
} ...

推荐阅读