{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# C++的谓词" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. 简介" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "① 返回bool类型的仿函数称为谓词。\n", "\n", "② 如果operator()接受一个参数,那么叫做一元谓词。\n", "\n", "③ 如果operator()接受两个参数,那么叫做二元谓词。" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 2. 一元谓词" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include\n", "using namespace std;\n", "#include\n", "#include\n", "\n", "//仿函数 返回值类型是bool数据类型,称为谓词\n", "//一元谓词\n", "class GreaterFive\n", "{\n", "public:\n", " bool operator()(int val)\n", " {\n", " return val > 5;\n", " }\n", "};\n", "\n", "void test01()\n", "{\n", " vectorv;\n", " for (int i = 0; i < 10; i++)\n", " {\n", " v.push_back(i);\n", " }\n", "\n", " //查找容器中,有没有大于5的数字\n", " //GreaterFive() 匿名函数对象\n", " vector::iterator it = find_if(v.begin(), v.end(), GreaterFive());\n", " if (it == v.end())\n", " {\n", " cout << \"未找到\" << endl;\n", " }\n", " else\n", " {\n", " cout << \"找到大于5的数字为:\" << *it << endl;\n", " }\n", "}\n", "\n", "int main() \n", "{\n", " test01();\n", " \n", " system(\"pause\");\n", "\n", " return 0;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "运行结果: \n", " - 找到大于5的数字为:6\n", " - 请按任意键继续. . ." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 3. 二元谓词" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include\n", "using namespace std;\n", "#include\n", "#include\n", "\n", "//仿函数 返回值类型是bool数据类型,称为谓词\n", "//二元谓词\n", "class MyCompare\n", "{\n", "public:\n", " bool operator()(int val1,int val2)\n", " {\n", " return val1 > val2;\n", " }\n", "};\n", "\n", "void test01()\n", "{\n", " vectorv;\n", " v.push_back(10);\n", " v.push_back(40);\n", " v.push_back(50);\n", " v.push_back(20);\n", " v.push_back(30);\n", "\n", " sort(v.begin(), v.end());\n", " for (vector::iterator it = v.begin(); it != v.end(); it++)\n", " {\n", " cout << *it << \" \";\n", " }\n", " cout << endl;\n", "\n", " //使用函数对象,改变算法策略,变为排序规则为从大到小\n", " sort(v.begin(), v.end(), MyCompare()); //MyCompare()为函数对象,是匿名函数\n", "\n", " cout << \"----\" << endl;\n", " for (vector::iterator it = v.begin(); it != v.end(); it++)\n", " {\n", " cout << *it << \" \";\n", " }\n", " cout << endl;\n", "}\n", "\n", "int main() {\n", "\n", " test01();\n", "\n", " system(\"pause\");\n", " \n", " return 0;\n", "\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "运行结果: \n", " - 10 20 30 40 50\n", " - $----$\n", " - 50 40 30 20 10\n", " - 请按任意键继续. . ." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.6.3", "language": "python", "name": "python3.6.3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": true } }, "nbformat": 4, "nbformat_minor": 4 }