本文主要是介绍C++ 函数对象仿函数谓词 tcy,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.1.说明:
1)用函数对象来替代函数指针,比函数指针功能强大(添加成员变量或函数)如在数组总寻找最接近某个值使用函数和函数指针没有整洁的方法做到
2)函数对象是完整的类类型对象,具有自己的成员变量1.2.分类:
1.2.1.基本函数对象class Less{public:bool operator()(int a, int b) const noexcept{return a < b;}};//函数调用运算符Less less; less(5,6)== less.oprator()(5,6)1.2.2.标准函数对象1)说明:a)对于普通函数可定义函数指针;无法创建内置运算符指针但可可创建函数对象b)标准库<functional>定义一系列函数模板,包括内置运算符如std::less<int>lessc)从C++14开始使用finctional函数对象类型推荐方法是省略类型参数确保在定义时没有显式指定类型实参的函数对象的行为与传统定义的函数对象完全相同在涉及隐式转换情况下用std::less<>比std::less<int>类型函数对象的代码更高效2)函数模板:比较运算符:less,greater,less_equal,greater_equal,equal_to,not_equal_to<>算数运算符:plus,minus,multiplies,divedes,modulus,negate<>逻辑运算符:logical_and,logical_or,logical_not<>按位运算符:bit_and,bit_or,bit_xor,bit_not<>
2.实例:一元仿函数
#include<algorithm>
#include<vector>
#include<numeric> //iota()用来填充容器递增方式using namespace std;class IncreasingNumberGenerator {public:int operator()()noexcept { return number++; }private:int number{ 0 };
};int main() {//被声明为const表明他的行为类似纯函数class ToSquare { public:constexpr int operator()(const int value) const noexcept { return value * value; } };vector<int> vec(6);//生成值分配给某个范围内的每个元素std::iota(vec.begin(), vec.end(), 0);//0 1 2 3 4 5 generate(begin(vec), end(vec), IncreasingNumberGenerator());//函数作用于容器中每个元素,并将结果存在另外一范围内transform(begin(vec), end(vec), begin(vec), ToSquare());
}
实例1.2:随机数生成器
#include<iostream>
#include<vector>
#include<random>using namespace std;template<typename T>
class RandomNumberGenerator {
public:RandomNumberGenerator() { mersenneTwisterEngine.seed(randomDevice()); }T operator()() { return distribution(mersenneTwisterEngine); }
private:random_device randomDevice;uniform_int_distribution<T> distribution;mt19937_64 mersenneTwisterEngine;
};using short_vector = vector<short>;short_vector createVectorRandom(int n=6) {RandomNumberGenerator<short> randomNumberGenerator{};short_vector vec(n);std::generate(begin(vec), end(vec), std::ref(randomNumberGenerator));return vec;
}void print(const short_vector& vec) {for (const auto& number : vec) cout << number << endl;
}int main() {short_vector vec = createVectorRandom();print(vec);
}
实例1.3:谓词:是一种特殊的仿函数,只有一个参数且返回bool
#include<iostream>
#include<vector>
#include<numeric> using namespace std;template<typename INT>
class isOdd {static_assert(is_integral<INT>::value, "not int");public:constexpr bool operator()(const int v)const noexcept { return (v % 2) != 0; }
};int main() {vector<int> vec(6);iota(begin(vec), end(vec),0);vec.erase(remove_if(begin(vec), end(vec), isOdd<int>()), end(vec));for (auto v : vec)cout << v << endl;
}
实例2:二元仿函数:有两个参数若返回bool则称为二元谓词
#include <iostream>
#include <vector>using namespace std;class Less {
public:bool operator()(int a, int b) const { return a < b; }
};template <typename T>
bool greaterEqual(const T& one, const T& other) { return one >= other; }class Nearer {
public:Nearer(int value) : n(value) {}bool operator()(int x, int y) const { return std::abs(x - n) < std::abs(y - n); }
private:int n;//存储要比较的值
};template <typename T, typename Comparison>
const T* find_optimum(const std::vector<T>& values, Comparison compare)
{if (values.empty()) return nullptr;const T* optimum = &values[0];for (size_t i = 1; i < values.size(); ++i){if (compare(values[i], *optimum)) optimum = &values[i];}return optimum;
}int main() {Less less; // Create a 'less than' functorstd::vector<int> numbers{ 91, 18, 92, 22, 13, 43 };std::cout << "Minimum element: " << *find_optimum(numbers, less) << std::endl;std::cout << "Maximum element: " << *find_optimum(numbers, greaterEqual<int>) << std::endl;int number_to_search_for{ 10 };std::cout << *find_optimum(numbers, Nearer{ number_to_search_for }) << std::endl;
}
这篇关于C++ 函数对象仿函数谓词 tcy的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!