Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates - Fatal编程技术网

C++ 排序、专门化的比较器

C++ 排序、专门化的比较器,c++,templates,C++,Templates,在模板函数中,std::vector应得到排序。T可以是简单类型或std::pair,例如 std::vector<double> or std::vector<std::pair<int,Something> > 当T是一对时,则仅比较第一个元素。如何实现这两种情况下的比较器 我试过: template<typename T> inline bool smaller(const T& a,const T& b) { r

在模板函数中,std::vector应得到排序。T可以是简单类型或std::pair,例如

std::vector<double> or 
std::vector<std::pair<int,Something> >
当T是一对时,则仅比较第一个元素。如何实现这两种情况下的比较器

我试过:

template<typename T>
inline bool smaller(const T& a,const T& b)
{
    return a<b;
}

template<typename T,typename S>
inline bool smaller(
    const std::pair<T,S>& a,
    const std::pair<T,S>& b
    )
{
    return a.first<b.first;
}

template<typename T> inline void function(std::vector<T >& vVec)
{
...bla...
sort(vVec.begin(),vVec.end(),smaller<T>);
...bla...
}

但它不是这样工作的。我也尝试过专门化,但没有找到专门化较小函数的正确语法。

您可以将其包装在lambda中:

std::sort(vVec.begin(),vVec.end(), [](const auto& a, const auto& b) { return smaller(a, b); });

你可以把它包在一个lambda里:

std::sort(vVec.begin(),vVec.end(), [](const auto& a, const auto& b) { return smaller(a, b); });

一个简单的解决方法是使两个较小的函数都成为较小的结构。使用

struct smaller
{
    template<typename T>
    bool operator()(const T& a,const T& b)
    {
        return a < b;
    }

    template<typename T, typename S>
    bool operator() (const std::pair<T, S>& a, const std::pair<T, S>& b)
    {
        return a.first < b.first;
    }
};

在排序中,重载解析将在两个较小的has运算符上生效,对于任何std::vector,都将调用std::pair重载。

一个简单的解决方法是使两个较小的函数都成为较小的结构。使用

struct smaller
{
    template<typename T>
    bool operator()(const T& a,const T& b)
    {
        return a < b;
    }

    template<typename T, typename S>
    bool operator() (const std::pair<T, S>& a, const std::pair<T, S>& b)
    {
        return a.first < b.first;
    }
};

在排序中,重载解析将在两个较小的运算符has上启动,对于任何std::vector,都将调用std::pair重载。

是否简单地尝试了sortvVec?相关:@Damien,sortvVec.begin,vVec.end还将比较该对的第二个元素。这在应用程序中是不必要的,并且第二个元素将需要一个运算符<。不必要的事实并不意味着这将是一个问题,当然,在这个条件下,您是否简单地尝试了运算符sortvVec?相关:@Damien,sortvVec.begin,vVec.end还将比较对中的第二个元素。这在应用中是不必要的,第二个元素需要一个运算符<。不必要的事实并不意味着这将是一个问题,当然,在运算符I只能接受一个答案作为解决方案的情况下,我选择了下面较短的lambda答案。你的回答也能解决这个问题,非常感谢。@Raffi不用担心。很高兴提供帮助。我只能接受一个答案作为解决方案,我选择了下面较短的lambda答案。你的回答也能解决这个问题,非常感谢。@Raffi不用担心。很乐意帮忙。