5.2.2 find_if

本文通过实例讲解C++标准库中的find_if函数,展示如何查找内置和自定义数据类型的元素,包括使用GreaterFive比较整数和Greater20筛选Person对象的年龄。

#include<iostream>
#include<vector>
using namespace std;
#include<functional>
#include<string>
#include<algorithm>

//查找算法 find_if


//查找内置数据类型
class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01()
{
	vector<int>v;
	for (int i = 0; i < 10;i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it = find_if(v.begin(),v.end(),GreaterFive());
	if (it == v.end())
	{
		cout << "没有找到" << endl;

	}
	else
	{
		cout << "找到了大于5的数字:" << *it << endl;
	}
}

//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

class Greater20
{
public:
	bool operator()(Person &p)
	{
		return p.m_Age > 20;
	}
};
void test02()
{
	vector<Person>v;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("eeee", 50);


	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	//找一个年龄大于20的人
	vector<Person>::iterator it = find_if(v.begin(),v.end(),Greater20());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
		{
		cout << "找到姓名:" << it->m_Name << "年龄:" << it->m_Age << endl;
	}
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

 

C:\**\**>npm install --global --production windows-build-tools npm warn config production Use `--omit=dev` instead. npm warn deprecated windows-build-tools@5.2.2: Node.js now includes build tools for Windows. You probably no longer need this tool. See https://github.com/felixrieseberg/windows-build-tools for details. npm warn deprecated uuid@3.4.0: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details. npm warn deprecated har-validator@5.1.5: this library is no longer supported npm warn deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142 npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful. npm warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported npm error code 1 npm error path D:\1Nodejs\node_global\node_modules\windows-build-tools npm error command failed npm error command C:\Windows\system32\cmd.exe /d /s /c node ./dist/index.js npm error Downloading python-2.7.15.amd64.msi npm error [> ] 0.0% (0 B/s) npm error ^[[1000D^[[0K^[[1A^[[1000D^[[0K^[[1A^[[1000D^[[0KDownloading python-2.7.15.amd64.msi npm error Error: GET https://npm.taobao.org/mirrors/python/2.7.15/python-2.7.15.amd64.msi returned 404 npm error Downloading Python failed. Error: Error: Could not find python-2.7.15.amd64.msi at https://npm.taobao.org/mirrors/python/2.7.15/python-2.7.15.amd64.msi npm error at nuggetCallback (D:\1Nodejs\node_global\node_modules\windows-build-tools\dist\download.js:60:35) npm error at done (D:\1Nodejs\node_global\node_modules\windows-build-tools\node_modules\nugget\index.js:58:9) npm error at Request.<anonymous> (D:\1Nodejs\node_global\node_modules\windows-build-tools\node_modules\nugget\index.js:197:58) npm error at Request.emit (node:events:508:28) npm error at Request.onRequestResponse (D:\1Nodejs\node_global\node_modules\windows-build-tools\node_modules\request\request.js:1059:10) npm error at ClientRequest.emit (node:events:508:28) npm error at HTTPParser.parserOnIncomingClient [as onIncoming] (node:_http_client:780:27) npm error at HTTPParser.parserOnHeadersComplete (node:_http_common:123:17) npm error at TLSSocket.socketOnData (node:_http_client:615:22) npm error at TLSSocket.emit (node:events:508:28) npm error windows-build-tools will now exit. npm error A complete log of this run can be found in: D:\1Nodejs\node_cache\_logs\2025-11-21T03_53_27_542Z-debug-0.log
11-22
<think> 我们面对两个主要问题: 1. node-gyp未安装或安装异常(表现为npm explore命令找不到node-gyp) 2. 安装windows-build-tools时下载python-2.7.15.amd64.msi出现404错误(因为Python2.7已经停止维护,官方可能移除了下载链接) 根据引用[1][2][3][4][5],我们可以采用以下步骤: 解决方案: 1. **修复node-gyp安装问题**: 首先确保node-gyp正确安装。如果之前安装出现问题,可以尝试重新安装。 ```bash npm uninstall -g node-gyp npm install -g node-gyp@latest ``` 2. **手动安装Python 2.7(解决404问题)**: 由于windows-build-tools默认下载的Python2.7.15可能已经不可用,我们可以手动安装Python2.7,并配置npm使用已安装的Python。 - 从非官方渠道(注意安全)或官方历史版本中下载Python2.7.15,例如: https://www.python.org/downloads/release/python-2715/ 选择“Windows x86-64 MSI installer”(64位)或“Windows x86 MSI installer”(32位)下载。 - 安装时注意勾选“Add python.exe to Path”(或安装后手动添加环境变量)。 - 验证安装:打开命令提示符,运行`python --version`应显示2.7.15。 3. **配置npm使用手动安装的Python**: ```bash npm config set python "C:\Python27\python.exe" # 替换为你的Python2.7安装路径 ``` 4. **安装windows-build-tools**: 由于手动安装了Python,我们可以跳过windows-build-tools中的Python安装部分,只安装Visual C++ Build Tools。可以尝试以下命令: ```bash npm install --global --production windows-build-tools --vs2015 ``` 或者,如果上述命令仍然尝试下载Python并失败,我们可以单独安装Visual Studio Build Tools: - 下载VS2015 Build Tools:https://download.microsoft.com/download/5/f/7/5f7acaeb-8363-451f-9425-68a90f98b238/visualcppbuildtools_full.exe - 安装时选择“自定义”,然后勾选“Visual C++”相关的构建工具。 5. **配置msbuild路径**(如果必要): 安装完成后,将MSBuild的路径添加到环境变量中。默认路径为: ``` C:\Program Files (x86)\MSBuild\14.0\Bin ``` 然后设置npm的msbuild路径: ```bash npm config set msbuild_path "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" ``` 6. **安装依赖**: 完成以上步骤后,尝试重新安装项目依赖: ```bash npm install ``` 7. **替代方案(推荐)**: 由于Python2.7已经不再受支持,我们可以尝试使用更高版本的构建工具(如VS2017或VS2019)以及Python3。node-gyp从v7开始支持Python3。因此,我们可以: - 安装Python3(>=3.6),并确保在环境变量中。 - 安装Visual Studio Build Tools 2019(安装时选择“使用C++的桌面开发”)。 - 配置npm使用Python3: ```bash npm config set python "C:\Python3x\python.exe" # 替换为你的Python3路径 ``` - 然后重新安装node-gyp: ```bash npm uninstall -g node-gyp npm install -g node-gyp@latest ``` - 这样,windows-build-tools可能不再需要,因为我们已经手动安装了构建环境。 8. **使用--ignore-scripts(临时方案)**: 如果只是为了避免安装时的编译错误,可以使用`--ignore-scripts`跳过编译步骤(但这不是根本解决办法,因为原生模块可能无法工作): ```bash npm install --ignore-scripts ``` 9. **使用cnpm(国内用户)**: 如果是由于网络问题导致下载失败,可以尝试使用cnpm(淘宝镜像): ```bash npm install -g cnpm --registry=https://registry.npm.taobao.org cnpm install --global --production windows-build-tools ``` 注意:以上步骤可能需要管理员权限运行命令提示符或PowerShell。 验证: 安装完成后,运行以下命令检查构建工具是否可用: ```bash node-gyp --version ``` 如果仍然遇到问题,请检查日志文件(如引用中提到的日志路径)。 引用说明: [^1]: 引用[1]提到安装windows-build-tools和node-gyp的命令,以及最后继续执行npm install。 [^2]: 引用[2]提到安装windows-build-tools时卡在安装Python2.7的问题。 [^3]: 引用[3]提到根据开发环境选择在线安装或手动安装,并查看npm全局配置。 [^4]: 引用[4]提到手动设置Python路径,以及使用`--ignore-scripts`安装特定版本的fibers模块。 [^5]: 引用[5]提到MSBUILD工具版本不匹配的问题。 根据这些引用,我们综合了手动安装Python、配置npm的python路径、安装Visual Studio Build Tools等步骤。 相关问题: 1. 如何卸载并重新安装Node.js和npm? 2. Windows上配置node-gyp有哪些特殊注意事项? 3. 如何解决npm全局安装路径冲突? 4. 如何验证windows-build-tools是否安装成功? 5. Node.js原生模块编译失败有哪些常见原因? 以上问题可根据上述解决方案扩展回答。 </think>### 解决 `windows-build-tools` 安装报错(Node-gyp未安装 & Python 2.7下载404) #### 问题分析 1. **Node-gyp 未安装**:npm 找不到全局安装的 node-gyp 模块 2. **Python 2.7 下载失败**:官方已停止维护 Python 2.7,下载链接失效 3. **构建环境不完整**:Windows 缺少 C++ 编译工具链 --- #### 完整解决方案(分步操作) ##### ✅ 步骤1:修复 Node-gyp 安装问题 ```bash # 1. 彻底卸载问题模块 npm uninstall -g node-gyp windows-build-tools --force # 2. 清除缓存 npm cache clean --force # 3. 重新安装 node-gyp(指定最新版) npm install -g node-gyp@latest --force ``` ##### ✅ 步骤2:手动安装 Python 2.7(解决 404 错误) 1. **下载 Python 2.7.15**: - 官方存档地址:https://www.python.org/downloads/release/python-2715/ - 选择 `Windows x86-64 MSI installer`(64位系统)或 `Windows x86 MSI installer`(32位系统) 2. **安装注意事项**: - 运行安装程序时勾选 **Add python.exe to Path** - 安装路径建议:`C:\Python27\` 3. **配置 npm 使用手动安装的 Python**: ```bash npm config set python "C:\Python27\python.exe" ``` ##### ✅ 步骤3:手动安装 Visual Studio Build Tools 1. **下载安装器**: - VS 2015 Build Tools:https://aka.ms/vs/14/release/vs_buildtools.exe 2. **安装选项**: - 勾选 **Visual C++ Build Tools** - 包含 **Windows 10 SDK**(版本 10.0.10240 或更高) ![安装界面](https://i.imgur.com/5XJzZ9l.png) 3. **配置环境变量**: ```bash npm config set msvs_version 2015 ``` ##### ✅ 步骤4:安装 windows-build-tools(绕过 Python 下载) ```bash # 使用 --no-python 跳过自动安装 npm install --global --production windows-build-tools --no-python ``` ##### ✅ 步骤5:验证安装 ```bash # 检查 node-gyp node-gyp --version # 检查构建工具 npm explore -g node-gyp -- node -p "require('fs').existsSync(require.resolve('windows-build-tools'))" # 应返回 true ``` --- #### 替代方案(推荐) 如果项目允许,**避免使用已废弃的 Python 2.7**: 1. 升级到支持 Python 3 的 node-gyp v7+: ```bash npm install -g node-gyp@latest npm config set python "C:\路径\to\python3.exe" ``` 2. 使用现代构建工具: - 安装 Visual Studio 2019 Build Tools(勾选 C++ 工具链) - 配置 npm: ```bash npm config set msvs_version 2019 ``` --- #### 关键说明 1. Python 2.7 已于2020年停止维护[^2],微软官方下载链接已失效 2. `windows-build-tools` 的自动安装脚本依赖已失效的 Python 下载源 3. 手动安装构建工具更可靠,且支持现代 Node.js 项目[^3][^5] > **提示**:遇到 MSBuild 工具版本错误时(如引用[5]),需确保安装的 VS Build Tools 版本与 npm 配置一致(`npm config set msvs_version 2015/2017/2019`)[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值