c++ - ISO C++ 禁止指针和整数之间的比较 [-fpermissive] [c++]
问题描述
ISO C++ 禁止指针和整数之间的比较 [-fpermissive]
if(S[i] == "#")
^~~
#include <iostream>
using namespace std;
int main() {
string S = "a#b#";
for( int i=0; i< S.length(); i++){
if(S[i] == "#")
//do somethng
}
return 0;
}
在谷歌上搜索这个错误时,我发现了一个解决方法,方法是使用“&”,if( &S[i] == "#")
它工作正常。有人可以告诉我为什么这有效,这里发生了什么?
解决方案
您正在迭代字符,但您将字符 (char) 与 (const char *) 进行比较。
您应该将其与字符“#”进行比较。
#include <iostream>
using namespace std;
int main() {
string S = "a#b#";
for( int i=0; i< S.length(); i++){
if(S[i] == '#') // here <--
//do somethng
}
return 0;
}
您可以将其简化为基于范围的 for 循环:
#include <iostream>
using namespace std;
int main() {
string S = "a#b#";
for(char character : S){
if(character == '#')
//do somethng
}
return 0;
}
推荐阅读
- javascript - 如何在 React.js 中正确进行条件渲染?
- flutter - 颤动@error:配置项目':google_sign_in'时出现问题
- pytorch - 如何在训练 CNN 时辨别哪张图像生成了特定的特征图?
- python - 过滤使用 generate_series 的查询同时保留空行
- linux - Linux 如何测量特定进程的经过时间?
- python - 从工作区中的 Jupyter Notebook 运行 Azure Auto-ML 实验时,假日国家/地区设置不起作用
- c# - 文档提取认知技能的使用
- asp.net-core - ASP.NET Core MVC 中的数字字段验证错误
- javascript - 无法在控制台中显示输入值
- pine-script - 如何隐藏绘图输入复选框