在家等待开工,看了本c++入门书,在看一篇文章中有介绍函数对象,自己搜了下,但是不清楚这种写法是什么意思:
template <class T>
struct less
: binary_function<T, T, bool> {
bool operator()(const T& x,
const T& y) const
{
return x < y;
}
};
这种写法中的binary_function<T, T, bool>是啥意思?
常见的函数对象写法:
class increment
{
private:
int num;
public:
increment(int n) : num(n) { }
int operator () (int arr_num) const {
return num + arr_num;
}
};
我理解的函数对象用法是:
increment(10);
//等价于
increment.operator()(10);
另外一个例子中也存在疑问:
#include <complex> // std::complex
#include <iostream> // std::cout/endl
#include <unordered_map> // std::unordered_map
#include <unordered_set> // std::unordered_set
using namespace std;
namespace std {
template <typename T>
struct hash<complex<T>> {
size_t
operator()(const complex<T>& v) const
noexcept
{
hash<T> h;
cout << "Hello,world"<<endl;
return h(v.real()) + h(v.imag());
}
};
} // namespace std
int main()
{
unordered_set<int> s{
1, 1, 2, 3, 5, 8, 13, 21
};
unordered_map<complex<double>,
double>
umc{{{1.0, 1.0}, 1.4142},
{{3.0, 4.0}, 5.0}};
return 0;
}
这里我在hash中添加了打印,结果打印了三次hello,world.
但是Unorder_map中只有两个元素,讲道理两个元素插入,计算hash也是计算两次呀,为什么打印三次呢?
std::binary_function 是一个二元函数对象的模板类。可以用作二元函数对象的基类。
不过再 C++17 里已经把这个给删了,所以最后不要再用这个了,直接写函数对象就好了。
C++ 本身并没有要求算几次。
这个算几次是要看算法实现的,不同的算法计算的次数不一定一样的。想知道细节,只能取看代码了。
stdlibc++ 里,每出现一个新的桶(没有 rehash 的时候)(除了第一元素),内部数据结构会有调整,需要多计算一个(已存在元素的)hash。