C++ C++;在CLion IDE中运行但不在Leetcode.com上运行的代码
我理解溢出作为一个概念的基本思想,但不知道如何确定它发生在哪里。 任何有助于理解这些问题的人都将不胜感激! 作为比较,下面是在我的IDE CLion中完美运行的代码:C++ C++;在CLion IDE中运行但不在Leetcode.com上运行的代码,c++,C++,我理解溢出作为一个概念的基本思想,但不知道如何确定它发生在哪里。 任何有助于理解这些问题的人都将不胜感激! 作为比较,下面是在我的IDE CLion中完美运行的代码: Line 506: Char 9: runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff (basic_string.h) //CLion版本 #包括 int main(){ std::s
Line 506: Char 9: runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff (basic_string.h)
//CLion版本
#包括
int main(){
std::string s=“MMMCMXCIX”;
//std::cout>s;
字符符号[7]={'I','V','X','L','C','D','M'};
int值[7]={1,5,10,501005001000};
int=0;
内部温度=0;
对于(int i;i STD::CUT看一下如何索引字符串和数组,并考虑您使用的最大索引是什么,尤其是当使用<代码> + 1 < /代码>时,如<代码> S[I+2] 和<代码>符号[TEMP+1 ]。
等等。然后将其与这些字符串/数组的大小进行比较。你看到问题了吗?如果你将数组或字符串索引超出了范围,那么你的程序有未定义的行为,并且不能保证它会做正确的事情。想想最后一次运行循环->s[i+1]时会发生什么情况。这将指向数组的超出范围。感谢你们两位的宝贵意见。
int romanToInt(string s) {
std::string a;
std::cout << "Please enter a roman numeral: ";
std::cin >> a;
Line 506: Char 9: runtime error: pointer index expression with base 0x000000000000 overflowed to 0xffffffffffffffff (basic_string.h)
// CLion Version
#include <iostream>
int main() {
std::string s = "MMMCMXCIX";
//std::cout << "Please enter a roman numeral: ";
//std::cin >> s;
char symbol[7] = {'I','V','X','L','C','D','M'};
int value[7] = { 1 , 5 , 10, 50,100,500,1000};
int int_ = 0;
int temp = 0;
for (int i; i < s.length(); i++) {
for (int j = 0; j < 7; j++) {
if (symbol[j] == s[i]) {
temp = j;
}
}
if (s[i+1] == symbol[temp + 1]) {
int_ = int_ + (value[temp + 1] - value[temp]);
i++;
}
else if (s[i+1] == symbol[temp + 2]) {
int_ = int_ + (value[temp + 2] - value[temp]);
i++;
}
else {
int_ = int_ + value[temp];
}
}
std::cout << int_;
return 0;
}