0
當我運行下面的代碼,我收到這樣的錯誤:運行此代碼時,爲什麼會收到std :: bad_alloc異常?
終止叫做拋出 '的std :: bad_alloc的'
什麼()的一個實例後:性病:: bad_alloc的這應用程序已經請求運行時以不常見的方式終止它。請聯繫應用程序的支持團隊獲取更多 信息。
我認爲這可能是由「resize()」行造成的,但我不知道如何解決它。這是我的代碼:
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
long long n, l, r;
string x[3];
int main()
{
ios::sync_with_stdio(false);
for (int i = 0; i <= 2; i++)
x[i].resize(x[i].max_size());
x[0] = '0';
x[1] = '1';
cin >> n >> l >> r;
for (register long long i = 2; i <= n ; i++)
x[i % 3] = x[(i - 2) % 3] + x[(i - 1) % 3];
cout << x[n % 3].substr(l, r - l + 1) << endl;
return 0;
}
不要調整字符串的大小。您是否嘗試打印出您嘗試調整大小的尺寸? –
你根本不需要調整它們的大小。 'c = a + b'會自動在'c'中分配足夠的存儲空間。 – aschepler
是的,它是2147483647 – godwings