[英]String std::logic_error: basic_string::_S_construct null not valid
#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;
}
The error pool statement is:错误池语句是:
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
After you fix the hard error of string S = 0;
修复
string S = 0;
的硬错误后string S = 0;
, you also have undefined behaviour, as you use T
without initialising it. ,你也有未定义的行为,因为你使用
T
而不初始化它。
Also using namespace std;
还
using namespace std;
pulls in far too many names, you shouldn't do it .引入太多名字,你不应该这样做。
#include <iostream>
#include <string>
int main()
{
std::string S;
int R;
std::cin >> S >> R;
for(int i = 0, T = 1/*???*/; i < T; i++)
{
for(char c : S)
{
for(int j = 0; j < R; j++)
{
std::cout << c;
}
}
std::cout << std::endl;
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.