Runtime Error 2.104505ms 404KiB foo.cc: In function 'bool check1(std::string)': foo.cc:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 16 | for(int i=1; i<s.size();i++) | ~^~~~~~~~~ foo.cc: In function 'bool check2(std::string)': foo.cc:24:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 24 | for (int i = 0; i < s.size(); i++) | ~~^~~~~~~~~~ foo.cc: In function 'int main()': foo.cc:54:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 54 | for (int i = 0; i < s.size()- 1; i++) | ~~^~~~~~~~~~~~~ foo.cc:59:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 59 | while(j < s.size() && s[j] >= '0' && s[j] <= '9') n = n * 10 + (s[j++] - '0'); | ~~^~~~~~~~~~ foo.cc:79:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 79 | while(j < s.size() && isdigit(s[j])) n = n * 10 + (s[j++]-'0'); | ~~^~~~~~~~~~ ExitCode: 11 (Segmentation fault)
时间: 2024-04-17 19:26:21 浏览: 95
非常抱歉,我之前提供的修正代码仍然有问题。根据您提供的错误信息,看起来可能是由于数组越界导致的段错误。经过仔细检查,我发现在代码中有一处错误。
请将以下两处代码:
```cpp
for (int i = 0; i < s.size()- 1; i++)
```
修改为:
```cpp
for (int i = 0; i < s.size(); i++)
```
这样修改后,代码应该可以正常执行,而不会出现段错误。
另外,还需要对 `check1` 函数中的循环条件进行修改。将 `i < s.size()` 修改为 `i < s.size() - 1`,这样可以避免比较越界。
再次感谢您的耐心和指出问题的细节。如果您还有其他问题,请随时提问。
相关问题
Runtime Error 2.012052ms 404KiB foo.cc: In function 'bool check1(std::string)': foo.cc:16:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 16 | for(int i=1; i<s.size()-1;i++) | ~^~~~~~~~~~~ foo.cc: In function 'bool check2(std::string)': foo.cc:24:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 24 | for (int i = 0; i < s.size()- 1; i++) | ~~^~~~~~~~~~~~~ foo.cc: In function 'int main()': foo.cc:54:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 54 | for (int i = 0; i < s.size()- 1; i++) | ~~^~~~~~~~~~~~~ foo.cc:59:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 59 | while(j < s.size() && s[j] >= '0' && s[j] <= '9') n = n * 10 + (s[j++] - '0'); | ~~^~~~~~~~~~ foo.cc:79:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 79 | while(j < s.size() && isdigit(s[j])) n = n * 10 + (s[j++]-'0'); | ~~^~~~~~~~~~ ExitCode: 11 (Segmentation fault)
非常抱歉,之前的代码存在一些问题。根据您提供的错误信息,看起来可能是由于数组越界导致的段错误。经过仔细检查,我发现在代码中有一处错误。
请将以下两处代码:
```cpp
for (int i = 0; i < s.size()- 1; i++)
```
修改为:
```cpp
for (int i = 0; i < s.size(); i++)
```
这样修改后,代码应该可以正常执行,而不会出现段错误。
再次感谢您的耐心和指出问题的细节。如果您还有其他问题,请随时提问。
Accepted 2.05215ms 404KiB foo.cc: In function 'std::string infixToPostfix(std::string)': foo.cc:19:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare] 19 | for (int i = 0; i < expression.length(); i++) { | ~~^~~~~~~~~~~~~~~~~~~~~
感谢您提供的提示。警告是由于在 `for` 循环中,将 `int` 类型的 `i` 与 `expression.length()` 的返回类型 `std::__cxx11::basic_string<char>::size_type` 进行比较,两者类型不同导致的。
为了解决这个警告,您可以将 `i` 的类型更改为与 `expression.length()` 返回类型相同的类型,即 `std::__cxx11::basic_string<char>::size_type`。修改后的代码如下:
```cpp
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int precedence(char op) {
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return 0;
}
string infixToPostfix(string expression) {
string postfix = "";
stack<char> stk;
stk.push('#');
for (std::__cxx11::basic_string<char>::size_type i = 0; i < expression.length(); i++) {
char c = expression[i];
if (isalnum(c)) {
postfix += c;
} else if (c == '(') {
stk.push('(');
} else if (c == ')') {
while (stk.top() != '#' && stk.top() != '(') {
postfix += stk.top();
stk.pop();
}
stk.pop();
} else {
while (stk.top() != '#' && precedence(c) <= precedence(stk.top())) {
postfix += stk.top();
stk.pop();
}
stk.push(c);
}
}
while (stk.top() != '#') {
postfix += stk.top();
stk.pop();
}
return postfix;
}
int main() {
string expression;
cin >> expression;
string postfix = infixToPostfix(expression);
cout << postfix << endl;
return 0;
}
```
通过将 `i` 的类型更改为 `std::__cxx11::basic_string<char>::size_type`,可以消除警告。
请再次运行代码,看看警告是否消失。如果您还有其他问题,请随时提问。
阅读全文