0

嗨,我 在以下代码中遇到了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”

4

3 回答 3

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 += ","
于 2012-06-20T06:13:44.640 回答
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;
于 2012-06-20T06:43:25.190 回答
0

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

您可能应该更改以orig_str从一开始就保持原样(否则它的命名非常糟糕)并将结果放入一个新字符串中。

于 2012-06-20T06:02:15.870 回答