仿函数介绍及用法

仿函数(Functor)是一种类对象,其实例可以像函数一样调用并返回结果。仿函数是一种重载了 operator() 的类,它向用户提供一个“函数调用”的接口,使得它的对象看起来像函数一样。

仿函数常用在STL算法中,作为函数对象传递给算法,以实现特定的操作。 STL 算法使用函数对象的原因是,它们可以轻松地在算法中插入特定的功能,因为函数对象在语法和语意上比函数更具有灵活性。

以下是使用仿函数的示例:

```c++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// 定义一个仿函数类对象
class greater_than {
public:
  explicit greater_than(int val) : m_val(val) {}

  bool operator()(int other) const {
    return other > m_val;
  }

private:
  int m_val;
};

int main() {
  vector<int> vec = {1, 5, 3, 4, 2};

  // 使用仿函数
  int count = count_if(vec.begin(), vec.end(), greater_than(3));

  cout << "Count: " << count << endl;   // 输出 Count: 2
  return 0;
}
```

上述代码定义了一个名为 greater_than 的仿函数类,其实例可以用于计算一个整数向量中大于某个数值的元素个数。 main() 函数中使用 count_if 算法来计算 vec 向量中大于 3 的元素个数,这里仿函数 greater_than(3) 作为count_if函数的一个参数传入。仿函数(Functor)是一种类对象,其实例可以像函数一样调用并返回结果。仿函数是一种重载了 operator() 的类,它向用户提供一个“函数调用”的接口,使得它的对象看起来像函数一样。

仿函数常用在STL算法中,作为函数对象传递给算法,以实现特定的操作。 STL 算法使用函数对象的原因是,它们可以轻松地在算法中插入特定的功能,因为函数对象在语法和语意上比函数更具有灵活性。

以下是使用仿函数的示例:

```c++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// 定义一个仿函数类对象
class greater_than {
public:
  explicit greater_than(int val) : m_val(val) {}

  bool operator()(int other) const {
    return other > m_val;
  }

private:
  int m_val;
};

int main() {
  vector<int> vec = {1, 5, 3, 4, 2};

  // 使用仿函数
  int count = count_if(vec.begin(), vec.end(), greater_than(3));

  cout << "Count: " << count << endl;   // 输出 Count: 2
  return 0;
}
```

上述代码定义了一个名为 greater_than 的仿函数类,其实例可以用于计算一个整数向量中大于某个数值的元素个数。 main() 函数中使用 count_if 算法来计算 vec 向量中大于 3 的元素个数,这里仿函数 greater_than(3) 作为count_if函数的一个参数传入。仿函数(Functor)是一种类对象,其实例可以像函数一样调用并返回结果。仿函数是一种重载了 operator() 的类,它向用户提供一个“函数调用”的接口,使得它的对象看起来像函数一样。

仿函数常用在STL算法中,作为函数对象传递给算法,以实现特定的操作。 STL 算法使用函数对象的原因是,它们可以轻松地在算法中插入特定的功能,因为函数对象在语法和语意上比函数更具有灵活性。

以下是使用仿函数的示例:

```c++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// 定义一个仿函数类对象
class greater_than {
public:
  explicit greater_than(int val) : m_val(val) {}

  bool operator()(int other) const {
    return other > m_val;
  }

private:
  int m_val;
};

int main() {
  vector<int> vec = {1, 5, 3, 4, 2};

  // 使用仿函数
  int count = count_if(vec.begin(), vec.end(), greater_than(3));

  cout << "Count: " << count << endl;   // 输出 Count: 2
  return 0;
}
```

上述代码定义了一个名为 greater_than 的仿函数类,其实例可以用于计算一个整数向量中大于某个数值的元素个数。 main() 函数中使用 count_if 算法来计算 vec 向量中大于 3 的元素个数,这里仿函数 greater_than(3) 作为count_if函数的一个参数传入。仿函数(Functor)是一种类对象,其实例可以像函数一样调用并返回结果。仿函数是一种重载了 operator() 的类,它向用户提供一个“函数调用”的接口,使得它的对象看起来像函数一样。

仿函数常用在STL算法中,作为函数对象传递给算法,以实现特定的操作。 STL 算法使用函数对象的原因是,它们可以轻松地在算法中插入特定的功能,因为函数对象在语法和语意上比函数更具有灵活性。

以下是使用仿函数的示例:

```c++
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

// 定义一个仿函数类对象
class greater_than {
public:
  explicit greater_than(int val) : m_val(val) {}

  bool operator()(int other) const {
    return other > m_val;
  }

private:
  int m_val;
};

int main() {
  vector<int> vec = {1, 5, 3, 4, 2};

  // 使用仿函数
  int count = count_if(vec.begin(), vec.end(), greater_than(3));

  cout << "Count: " << count << endl;   // 输出 Count: 2
  return 0;
}
```

上述代码定义了一个名为 greater_than 的仿函数类,其实例可以用于计算一个整数向量中大于某个数值的元素个数。 main() 函数中使用 count_if 算法来计算 vec 向量中大于 3 的元素个数,这里仿函数 greater_than(3) 作为count_if函数的一个参数传入。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻戈素笺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值