简体   繁体   English

字符串 std::logic_error: basic_string::_S_construct null 无效

[英]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

string S = 0; is interpreted as std::string S{nullptr};被解释为std::string S{nullptr}; which is forbidden .这是被禁止的

Just write string S;只写string S; instead.反而。

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.

相关问题 std :: logic_error:basic_string :: _ s_construct null无效 - std::logic_error: basic_string::_s_construct null not valid 抛出&#39;std :: logic_error&#39;what()实例后调用终止终止:basic_string :: __ S_construct null无效 - terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 如何避免错误:在抛出&#39;std :: logic_error&#39;的实例后调用terminate what():basic_string :: _ S_construct null无效 - How to avoid the error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid 为什么在抛出'std :: logic_error'what()的实例后显示终止调用:basic_string :: _S_construct null无效 - Why does it show terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid std :: string(empty char *)错误:basic_string :: _ S_construct null无效错误 - std::string(empty char*) error: basic_string::_S_construct null not valid error 在函数参数/套接字中发送std :: string时basic_string :: _ S_construct null无效错误 - basic_string::_S_construct null not valid error while sending std::string in function argument/socket 如何修复 &#39;std::logic_error&#39; what(): basic_string::_M_construct null not valid 错误? - How to fix 'std::logic_error' what(): basic_string::_M_construct null not valid error? 'std::logic_error' what(): basic_string::_M_construct null 无效 - 'std::logic_error' what(): basic_string::_M_construct null not valid basic_string :: _ S_construct null无效 - basic_string::_S_construct null not valid openCV basic_string :: _ S_construct null无效 - openCV basic_string::_S_construct null not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM