c++ - 如何检查结构类型或字段是否存在

标签 c++ function struct template-function

我有以下代码来计算“倍数” vector 的最大绝对值(表示倍数的结构)——在我的例子中,我有 std::pair 和我自己的 Triple 结构,其功能与前者基本相同,但具有 3 个字段。

/**
 * @brief Computes the maximum absolute value of a vector of specified structs 
 *
 * Iterates through all elements of a vector checking the T.first, T.second and T.third
 * values to find the abs maximum element of the data structure.
 *
 * @param data Vector of pairs of integers
 * @param elementOfMax Pointer to integer which will store the element (1,2 or 3) that the maximum 
 * value of the vector of T structs is contained within, pass the address of an int variable as this param.
 * @param coordChoice [= 0] Optional argument to choose specific 'x' or 'y' co-ordinate
 * of the T struct to compute maximum for - set coordChoice to 1 for 1st element, 2
 * for 2nd element (etc.) any other value will result in all elements being considered.
 * @return maximum value of data
 */
template<typename T> int absMaxOfVectorOfMultiples(std::vector< T >& data, int* elementOfMax, int coordChoice = 0) {

    // set initial maximum to 0
    int maximum = 0;
    *elementOfMax = 0;

    bool isPair = false;

    if (typeid(T).name() == typeid(std::pair<int, int>).name()) {
        isPair = true;
    }

    // loop over all elements in the data vector
    for (unsigned int i = 0; i < data.size(); i++) {

        if (coordChoice != 2 && coordChoice != 3) {

            // if the first element of the multipe struct at this data point
            // is greater than current maximum, set this element
            // to the new maximum value
            if (std::abs(data.at(i).first) > maximum) {
                maximum = data.at(i).first;
                *elementOfMax = 1;
            }
        }

        if (coordChoice != 1 && coordChoice != 3) {

            // if the second element of the multiple struct at this data point
            // is greater than current maximum, set this element
            // to the new maximum value
            if (std::abs(data.at(i).second) > maximum) {
                maximum = data.at(i).second;
                *elementOfMax = 2;
            }
        }

        if (!isPair) {

            if (coordChoice != 1 && coordChoice != 2) {

                // if the third element of the multtiple struct at this data point
                // is greater than current maximum, set this element
                // to the new maximum value
                if (std::abs(data.at(i).third) > maximum) {
                    maximum = data.at(i).third;
                    *elementOfMax = 3;
                }


            }


        }

    }

return maximum;

}

尽管尚未对此进行测试,但我知道在将 std::pair 结构传递给函数时这将不起作用,因为一对中没有 third 字段。如果传递的结构是 Triple,我将如何更改此代码,以便获取和检查 third 字段的代码块仅“可用”并执行?

最佳答案

您可以为不同的部分编写 2 个重载:

void absMaxOfVectorOfMultiples_third(std::pair<int, int>& data,
                                     int& maximum,
                                     int* elementOfMax,
                                     int coordChoice)
{
    // empty.
}

void absMaxOfVectorOfMultiples_third(Triple& data,
                                     int& maximum,
                                     int* elementOfMax,
                                     int coordChoice)
{
    if (coordChoice != 1 && coordChoice != 2) {

        // if the third element of the multiple struct at this data point
        // is greater than current maximum, set this element
        // to the new maximum value
        if (std::abs(data.third) > maximum) {
            maximum = data.third;
            *elementOfMax = 3;
        }
    }
}

然后主要功能将是

template<typename T>
int absMaxOfVectorOfMultiples(std::vector<T>& data, int* elementOfMax, int coordChoice = 0)
{
    // set initial maximum to 0
    int maximum = 0;
    *elementOfMax = 0;

    // loop over all elements in the data vector
    for (unsigned int i = 0; i < data.size(); i++) {
        if (coordChoice != 2 && coordChoice != 3) {

            // if the first element of the multiple struct at this data point
            // is greater than current maximum, set this element
            // to the new maximum value
            if (std::abs(data.at(i).first) > maximum) {
                maximum = data.at(i).first;
                *elementOfMax = 1;
            }
        }
        if (coordChoice != 1 && coordChoice != 3) {

            // if the second element of the multiple struct at this data point
            // is greater than current maximum, set this element
            // to the new maximum value
            if (std::abs(data.at(i).second) > maximum) {
                maximum = data.at(i).second;
                *elementOfMax = 2;
            }
        }
        absMaxOfVectorOfMultiples_third(data.at(i), maximum, elementOfMax, coordChoice);
    }
    return maximum;
}

关于c++ - 如何检查结构类型或字段是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34472068/

相关文章:

performance - 使用内联会降低性能吗?

c - 指针分配后错误的结构成员值

c - 初始化 const struct 并明确哪个字段是哪个

c++ - 使用 union (封装在结构中)绕过 neon 数据类型的转换

c++ - RPC 身份验证

function - 如何在 Vim 命令行中输入文本而不执行它?

ios - 尝试使函数快速返回列表时出现错误

c++ - 在 C++ 中将字符串转换为 LPVOID 和 LPCWSTR

c++ - opengl绘制窗口行高

c - 此 C 继承实现是否包含未定义的行为?