c++ - 如何将某个字符与字符串的另一个字符进行比较
问题描述
在以下代码中,我无法比较两个给定字符串的特定字母。
#include <bits/stdc++.h>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
cin.ignore();
string phrases[m];
string records[n];
for (int i = 0; i < m; i++) {
getline(cin, phrases[i]);
}
for (int i = 0; i < n; i++) {
getline(cin, records[i]);
}
int lowBound;
sort(phrases, phrases + m);
int ans = 0;
bool stillIs;
for (int i = 0; i < n; i++) {
lowBound = lower_bound(phrases, phrases + m, records[i]) - phrases;
if (lowBound == m) {
continue;
}
stillIs = true;
for (int j = 0; j < records[i].length(); i++) {
if (records[i][j] == phrases[lowBound][j]) {
stillIs = false;
}
}
if (stillIs) {
ans++;
}
}
cout << ans;
return 0;
}
在第 33 行,如果 (records[i][j] == phrases[lowBound][j]),它不会给我一个错误,但是如果我用这一行运行它,什么都不会发生,但是当我评论 if声明出来,它工作正常,但显然没有给我正确的答案。有什么办法可以比较这两个字符串(第二个字符串比第一个字符串大)来确定第一个字符串是否是第二个字符串的开头?
谢谢!
解决方案
- 您正在递增
i
而不是j
在第 32 行的循环中 - 你在第 33 行有你的测试 -
stillIs = false
如果字符不匹配,你想设置,(即,!=
)
我还没有完全阅读你的代码,但是这两个问题突然出现在我身上,所以看看是否能解决它
推荐阅读
- spring-boot - Hystrix假装重试超时不起作用
- r - 如何显示每种科目在不同时期的速度进展
- php - PHP - 如何在 foreach 循环中按字母顺序排序名称而不是 ID?
- javascript - 如何检查我推入对象的对象数组是否具有来自另一个对象数组的特定键值
- azure - 生成的工件文件夹层数过多
- python - tesseract 用阿拉伯语返回颠倒的单词
- java - 如何在 Hibernate 上使用 Redis 作为 L2 缓存?
- c# - 在 ASP.NET Core 3.1 中使用 SmtpClient 发送电子邮件附件给出“无法访问关闭的流”。
- mysql - mysql查询中的括号给出不同的结果
- reactjs - 如何将下拉选择设置为 localStorage?