c++14 - 无法在 c++14 中比较点和整数
问题描述
我正在尝试编写代码解码器。但是对于 if 语句中的所有比较,我得到以下错误:
'错误:ISO C++ 禁止指针和整数之间的比较 [-fpermissive]'
输入字符串的示例是".-.--"
和"-..-.--"
。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
int c[100], t = 0, l, i = 0;
l = s.length();
cin >> s;
if (s[0] == '.') {
c[0] = 0;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.') {
c[t] = 1;
t += 1;
i += 2;
}
if (s[i] == '.') {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == '-' && s[i + 1] == '-') {
c[t] = 2;
t += 1;
i += 2;
}
}
}
if (s[0] == '-' && s[1] == '.') {
c[0] = 1;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.'
'){
c[t] = 1; t += 1; i += 2;
}
if (s[i] == '.') {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == '-' && s[i + 1] == '-') {
c[t] = 2;
t += 1;
i += 2;
}
}
}
if (s[0] == '-' && s[1] == '-') {
c[0] = 2;
t += 1;
while (i < l) {
if (s[i] == '-' && s[i + 1] == '.') {
c[t] = 1;
t += 1;
i += 2;
}
if (s[i] == ".") {
c[t] = 0;
t += 1;
i++;
}
if (s[i] == "-" && s[i + 1] == "-") {
c[t] = 2;
t += 1;
i += 2;
}
}
}
for (i = 0; i < t; i++) {
cout << s[t];
}
return 0;
}
我该如何解决这个问题?
解决方案
在您到达这里之前,您一直在使用单引号:
if(s[i]=="-"&&s[i+1]=="-"){
您需要将其更改为单引号,以便进行 int 到 int 的比较。
if(s[i]=='-'&&s[i+1]=='-'){
当你说
"-"
您正在创建一个指针。当你说
'='
您正在创建一个 int。
推荐阅读
- ffmpeg - FFMPEG php HLS 和 DASH 转换
- python - 我想根据 pandas 列发送电子邮件
- javascript - 使用 nodejs 库为 google 经销商 api 验证服务帐户时出现问题
- android - 如何使用推送器发送数据类型映射?
- sas - PROC FCMP 返回不正确的字符串长度
- firebase - 上传用户个人资料图片 Firebase - Flutter
- ios - UICollectionViewCompositionalLayout - 部分作为列
- c# - 创建 PDF 适用于 Unity (Windows) 但不适用于 Android 设备
- google-cloud-platform - 如何为免费的谷歌云功能缓存一个低于 5MB 的配置?
- java - hamcrest-core: jar:1.4-atlassian-1 无法在 Spring Boot 项目的 MVN 全新安装上解决