- 函数进化()
函数 -> 函数指针 -> 函数模板 -> 仿函数->lambda表达式
文章目录
- 1 函数
- 2 函数指针
- 3 函数模板
- 4 仿函数(函数对象)
- 5 lambda表达式简化
1 函数
#include
using std::cin;
using std::cout;
using std::string;
typedef int* pInt;
int Count_Match20_Elem(pInt pSta, pInt pEnd) {
int res = 0;
for (; pSta != pEnd; ++pSta)
if (*pSta > 20)
res++;
return res;
}
int Count_Match25_Elem(const pInt pSta, const pInt pEnd) {
int res = 0;
for (; pSta != pEnd; ++pSta)
if (*pSta > 25)
res++;
return res;
}
int main(int* argc, char* argv[]){
int arr[] = { 11,16,21,19,17,30 };
cout << Count_Match20_Elem(arr, arr + sizeof(arr) / 4);
return 0;
}
2 函数指针
#include
using std::cin;
using std::cout;
using std::string;
bool isGreater20(const int& val) { return val > 20; }
bool isGreater25(const int& val) { return val > 25; }
int CountMatchElem(int* pSta, int* pEnd, bool(*pComp)(const int&)) {
int res = 0;
for (; pSta != pEnd; ++pSta)
if (pComp(*pSta))
res++;
return res;
}
int main(int* argc, char* argv[]){
int arr[] = { 11,16,21,19,17,30 };
cout << CountMatchElem(arr, arr + sizeof(arr)/ sizeof(int), isGreater20);
return 0;
}
3 函数模板
#include
using std::cin;
using std::cout;
using std::string;
bool isGreater20(const int& val) { return val > 20; }
bool isGreater25(const int& val) { return val > 25; }
bool isTinyStr(const string& str) { return str.size() <= 3; }
template<typename DataType>
int CountMatchElem(DataType* pSta, DataType* pEnd, bool(*pComp)(const DataType&)) {
int res = 0;
for (; pSta != pEnd; ++pSta)
if (pComp(*pSta))
res++;
return res;
}
int main(int* argc, char* argv[]){
int arr[] = { 11,16,21,19,17,30 };
string strs[] = { "abc", "bcde", "cdefg", "de", "efg" };
cout << CountMatchElem<int>(arr, arr + sizeof(arr)/ sizeof(int), isGreater20);
cout << CountMatchElem<string>(strs, strs + sizeof(strs) / sizeof(strs[0]), isTinyStr);
return 0;
}
4 仿函数(函数对象)
#include
using std::cin;
using std::cout;
using std::string;
template<typename T>
struct Greater {
T StdVal;
explicit Greater(T val) : StdVal(val) {}
bool operator()(const T& val) const { return val > StdVal; }
};
template<typename DataType>
int CountMatchElem(DataType* pSta, DataType* pEnd, bool(*pComp)(const DataType&)) {
int res = 0;
for (; pSta != pEnd; ++pSta)
if (pComp(*pSta))
res++;
return res;
}
int main(int* argc, char* argv[]){
int arr[] = { 11,16,21,19,17,30 };
Greater<int> gtr20(20);
cout << CountMatchElem(arr, arr + sizeof(arr) / sizeof(int), gtr20);
return 0;
}
template<typename DataType, typename pFunc>
int CountMatchElem(DataType* pSta, DataType* pEnd, pFunc pComp)
5 lambda表达式简化
#include
using std::cin;
using std::cout;
using std::string;
template<typename DataType, typename pFunc>
int CountMatchElem(DataType* pSta, DataType* pEnd, pFunc pComp) {
int res = 0;
for (; pSta != pEnd; ++pSta)
if (pComp(*pSta))
res++;
return res;
}
int main(int* argc, char* argv[]){
int arr[] = { 11,16,21,19,17,30 };
auto gtr20 = [](auto& val) -> bool {return val > 20; };
cout << CountMatchElem(arr, arr + sizeof(arr) / sizeof(int), gtr20);
return 0;
}
- 参考视频源:【C++函数的进化 函数→函数指针→函数模板→仿函数|函数对象→lambda表达式】