doc/docs/CPlusPlus-main/31_C++的谓词.ipynb
sairate fa9377e4ae docs(book): 添加现代 C++教程及相关代码
- 新增现代 C++ 教程的 Preface 章节,包括英文和中文版本
- 添加 C++ Primer 练习代码
- 新增 Learn C++ 教程的 C++ 开发简介章节
- 添加头文件解析文档
- 更新 mkdocs.yml,包含新教程的目录结构
- 修改项目设置,使用 Python 3.10环境
2025-07-08 09:52:45 +08:00

211 lines
4.6 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"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<iostream>\n",
"using namespace std;\n",
"#include<vector>\n",
"#include<algorithm>\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",
" vector<int>v;\n",
" for (int i = 0; i < 10; i++)\n",
" {\n",
" v.push_back(i);\n",
" }\n",
"\n",
" //查找容器中有没有大于5的数字\n",
" //GreaterFive() 匿名函数对象\n",
" vector<int>::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<iostream>\n",
"using namespace std;\n",
"#include<vector>\n",
"#include<algorithm>\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",
" vector<int>v;\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<int>::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<int>::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
}