在此字符串操作代码中获取out_of_range错误

时间:2012-06-20 04:56:53

标签: c++ string

您好我正在点击 std :: out_of_range:basic_string :: substr 在以下代码中。一直在努力解决它,但无法完成它。

std::string orig_str = "1-1,2-3,4-4,56-75,77-77";
size_t number_digits;
for (size_t i = 0; i < orig_str.size(); ++i)
{
    number_digits = orig_str.find_first_of(",", i) - i - 1;
    if(orig_str.find("-", i) != std::string::npos)
    if (orig_str.substr(i - number_digits, i - 1) == orig_str.substr(i + 1, i + number_digits))
    orig_str.erase(i-number_digits, number_digits + 1);            
}

基本上这段代码正在操作字符串以删除某些范围 预期的行为是: 输入字符串:“0-0,2-7,88-88”输出字符串:“0,2-7,88”

3 个答案:

答案 0 :(得分:1)

AJ,这是我们在聊天中讨论的伪代码:

input_string = "1-1,2-3,4-4,56-75,77-77"
output_string = ""
while (input_string is not empty)
    sub = input_string.substr(up to first comma)
    input_string = input_string.substr(after first comma)
    if (sub.substr(up to dash) == sub.substr(after dash))
        output_string += sub.substr(up to dash)
    else
        output_string += sub
    if (input_string is not empty)
        output_string += ","

答案 1 :(得分:0)

您迭代原始字符串的长度,并修改它,从而更改长度。编译器可能会优化对orig_str.size()的调用,这意味着它将获取一次长度并允许您迭代超过字符串的新长度。

您应该更改为保持orig_str从头开始(否则命名非常严重)并将结果放入新字符串中。

答案 2 :(得分:0)

    int flag = 0;
    int num_ch;
    char * fnum = NULL, * snum = NULL;
    int numOfLoop = orig_str.size();
    for(int i = 0; i < numOfLoop; i++)
    {
        if(orig_str[i] == '-')
        {
            flag = 1;
        }
        else if(orig_str[i] == ',')
        {
            int ret = strcmp(fnum, snum);
            if(ret == 0)
            {
                int sizeOfNUM = strlen(fnum);
                int numOfChToReplace = sizeOfNUM + sizeOfNUM + 1;
                orig_str.replace(i - numOfChToReplace,numOfChToReplace, fnum); 
                numOfLoop = numOfLoop - (sizeOfNUM + 1);
                i = i - (sizeOfNUM + 1);
            }

            flag = 0;
            delete [] fnum;
            delete [] snum;
            fnum = NULL;
            snum = NULL;
        }
        else
        {
            if(flag == 1)
            {
                if(snum == NULL)
                {
                    snum = new char[2];
                    snum[0] = orig_str[i];
                    snum[1] = '\0';
                }
                else
                {
                    int size = strlen(snum);
                    char * tmp = snum;
                    snum = new char[size + 2];
                    for( int j = 0; j < size; j++)
                        snum[j] = tmp[j];
                    snum[size] = orig_str[i];
                    snum[size + 1] = '\0';
                }
            }
            else 
            {
                if(fnum == NULL)
                {
                    fnum = new char[2];
                    fnum[0] = orig_str[i];
                    fnum[1] = '\0';
                }
                else
                {
                    int size = strlen(fnum);
                    char * tmp = fnum;
                    fnum = new char[size + 2];
                    for( int j = 0; j < size; j++)
                        fnum[j] = tmp[j];
                    fnum[size] = orig_str[i];
                    fnum[size + 1] = '\0';
                }
            }
        }
    }

    int ret = strcmp(fnum, snum);
    if(ret == 0)
    {
        int sizeOfNUM = strlen(fnum);
        int numOfChToReplace = sizeOfNUM + sizeOfNUM + 1;
        orig_str.replace(i - numOfChToReplace,numOfChToReplace, fnum); 
        numOfLoop = numOfLoop - (sizeOfNUM + 1);
        i = i - (sizeOfNUM + 1);
    }

    flag = 0;
    delete [] fnum;
    delete [] snum;
    fnum = NULL;
    snum = NULL;

    std::cout << orig_str.c_str() << std::endl;