仿函数(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函数的一个参数传入。
6375

被折叠的 条评论
为什么被折叠?



