【发布时间】:2021-12-16 23:41:41
【问题描述】:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string S = 0;
int T,R;
cin >> S >> R;
for(int i = 0; i < T; i++)
{
for(int k = 0; k < S.length(); k++)
{
for(int j = 0; j < R; j++)
{
cout << S[k];
}
}
cout << endl;
}
return 0;
}
错误池语句为:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
【问题讨论】:
-
有趣的是,您会尝试在不必要的情况下将
std::string初始化为 0,但不初始化其他变量,这是一种很好的做法。由于T未初始化且从未分配,因此它可能不是用作循环限制的最佳选择。