libc ++ abi.dylib:终止于类型为std :: out_of_range的未捕获异常:basic_string错误

最初运行代码时没有错误。 在我输入Test string之后,I a a会给我:

libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string

我不知道这是关于字符串函数还是我的输入。

p.s。该程序用于删除,插入和替换字符串中的字母

我的代码在下面

using namespace std;

string str;
bool flag = true;

void del(char p)
{
    int pos = str.find(p);
    if (pos != string::npos)
    {
        str.erase(pos,1);
        return ;
    }
    else
    {
        cout << "N" << endl;
        flag = false;
    }
}

void ins(string p1,string p2)
{
    int pos = str.find(p1[0]);
    if (pos == string::npos)
    {
        cout << "N" << endl;
        flag = false;
        return ;
    }
    int newpos = pos;
    while (pos != string::npos)
    {
        pos = str.find(p1,newpos);
        newpos = pos + 1;
    }
    str.insert(pos,p2);
    return ;
}

void repl(string p1,string p2)
{
    int pos = 0;
    int find = 0;
    find = str.find(p1[0],pos);
    if (find == string::npos)
    {
        cout << "N" << endl;
        flag = false;
        return ;
    }
    while (true)
    {
        find = str.find(p1[0],pos);
        if (find != string::npos)
        {
            str[find] = p2[0];
            pos = find + 1;
        }
        else
        {
            break;
        }
    }
    return ;
}

int main()
{
    getline(cin,str);
    char a;
    cin >> a;
    //cout << a << endl;
    if (a == 'D')
    {
        char p1;
        cin >> p1;
        del(p1);
    }
    else if (a == 'I')
    {
        string p1,p2;
        cin >> p1 >> p2;
        ins(p1,p2);
    }
    else if (a == 'R')
    {
        string p1,p2;
        cin >> p1 >> p2;
        repl(p1,p2);
    }
    if (flag)
    {
        cout << str << endl;
    }
    return 0;
}

我在Mac上使用VS Code。我不知道是否有帮助。 我不知道可以输入什么,但是它告诉我我的帖子主要是代码,我需要添加更多详细信息...

aa4568213 回答:libc ++ abi.dylib:终止于类型为std :: out_of_range的未捕获异常:basic_string错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3160887.html

大家都在问