我是编码新手,遇到以下错误。
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at: __n (which is 6) >= this->size() (which is 6)
Aborted (core dumped)
以下代码是:
#include <iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
string sa=to_string(a);
string sb=to_string(b);
int l=sa.length();
for(int i=0;i<l;i++)
{
if(sa.at(i)==sb.at(i))
{
cout<<0;
}
else
cout<<1;
}
}
这个问题的输入是
1010100
0100101
任何帮助,将不胜感激!
由于前导零,因此读取第二个输入100101。
尝试访问尽可能多的1010100字符将超出其长度。
为了解决这两个读入作为字符串。
例如,如下所示(请注意,该代码仅演示了我建议的更改,它仍然容易受到不同的长输入之类的影响,100 10
并具有其他缺点):
string sa,sb;
cin>>sa>>sb;
/* no to_string() */
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句