通过引用或值传递比较函子

时间:2019-01-24 02:20:48

标签: c++ c++11 std

我注意到有些std类/函数按值接受函数参数,有些按引用接受函数。

例如,std::sort按值具有比较功能:

template<... class Compare>
void sort( ..., Compare comp )

和容器构造函数,例如std::set,通过引用获得比较功能:

template<... class Compare>
class set
{
    explicit set( const Compare& comp, ... )
    ...
}

为什么会有这种区别,在哪种情况下首选一种或另一种方法?

0 个答案:

没有答案