Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 仅使用C+;中的标准函数获取std::pair成员+;03_C++_C++03 - Fatal编程技术网

C++ 仅使用C+;中的标准函数获取std::pair成员+;03

C++ 仅使用C+;中的标准函数获取std::pair成员+;03,c++,c++03,C++,C++03,有没有办法只使用C++03标准函数获得std::pair成员,即第一个或第二个 在C++11中,我可以在这种情况下分别使用std::get或std::get。不,没有。如果您想要它们,您必须自己制作。没有免费函数允许您检索std::pair::first和std::pair::second。然而,实现以下目标并不重要: template <std::size_t TI, typename T> struct get_helper; template <typename T&g

有没有办法只使用C++03标准函数获得
std::pair
成员,即
第一个
第二个


在C++11中,我可以在这种情况下分别使用
std::get
std::get

不,没有。如果您想要它们,您必须自己制作。

没有免费函数允许您检索
std::pair::first
std::pair::second
。然而,实现以下目标并不重要:

template <std::size_t TI, typename T>
struct get_helper;

template <typename T>
struct get_helper<0, T>
{
    typedef typename T::first_type return_type;

    return_type operator()(T& pair) const
    {
        return pair.first;
    }
};

template <typename T>
struct get_helper<1, T>
{
    typedef typename T::second_type return_type;

    return_type operator()(T& pair) const
    {
        return pair.second;
    }
};

template <std::size_t TI, typename T>
typename get_helper<TI, T>::return_type my_get(T& pair)
{
    return get_helper<TI, T>()(pair);
}
模板
结构get_helper;
模板
结构获取辅助程序
{
typedef typename T::first_type返回_type;
返回类型运算符()
{
返回对。第一;
}
};
模板
结构获取辅助程序
{
typedef typename T::second_type返回_type;
返回类型运算符()
{
返回对。秒;
}
};
模板
typename get\u helper::return\u type my\u get(T&pair)
{
返回get_helper()(成对);
}