Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:使用迭代器替换部分字符串无效_C++_Visual Studio 2008_String_Iterator - Fatal编程技术网

C++ C++;:使用迭代器替换部分字符串无效

C++ C++;:使用迭代器替换部分字符串无效,c++,visual-studio-2008,string,iterator,C++,Visual Studio 2008,String,Iterator,我正在写一个简单的程序,它试图找到给定数字后的下一个回文数 至于现在,我被困在这一点上: string::iterator iter; // iterators for the string string::iterator riter; //testcases is a vector<string> with strings representing numbers. for (unsigned int i = 0; i < testcases.size() ; ++i

我正在写一个简单的程序,它试图找到给定数字后的下一个回文数

至于现在,我被困在这一点上:

string::iterator iter; // iterators for the string
string::iterator riter; 


//testcases is a vector<string> with strings representing numbers.
for (unsigned int i = 0; i < testcases.size() ; ++i) {
    iter = testcases[i].begin();
    riter = testcases[i].end();

    while ( !isPalin(testcases[i]) ) { //isPalin(string) is a function
                                       //which is checking if given string
                                       //is a palindrome

        //if n-th digit from the end is different from the
        //n-th digit, then I want to replace latter one, so they will 
        //be the same.
        if ( *iter != *riter ) {
            testcases[i].replace(riter, riter, *iter);
        }

        ++iter; // advancing forward iterator;
        --riter; // advancing backward iterator;
    }
    cout << testcases[i] << " -> ok\n";
}
string::iterator iter;//字符串的迭代器
string::迭代器riter;
//testcases是一个带有表示数字的字符串的向量。
for(无符号int i=0;i关于您拥有的代码:

为什么不在两个迭代器的末尾赋值呢

if ( *iter != *riter ) {
   *riter = *iter;
}
正如Oli指出的,代码中还有其他问题,其中第一个问题是,您将riter设置为string.end(),它是一个不可取消引用的迭代器。end()迭代器始终是一个超过末尾的迭代器,因此上面的使用将尝试写入指定内存之外的内容

也许您应该尝试使用.rbegin(),它将提供一个反向迭代器,指向在递增字符串时向字符串开头移动的最后一个元素

关于算法:


如果您打算查找下一个回文数字,我不确定您实现的算法是否正确。例如,如果输入数字为123456,算法将检测到它不是回文,并将转换为比原始数字小的12345_1_。

关于您的代码:

为什么不在两个迭代器的末尾赋值呢

if ( *iter != *riter ) {
   *riter = *iter;
}
正如Oli指出的,代码中还有其他问题,其中第一个问题是,您将riter设置为string.end(),它是一个不可取消引用的迭代器。end()迭代器始终是一个超过末尾的迭代器,因此上面的使用将尝试写入指定内存之外的内容

也许您应该尝试使用.rbegin(),它将提供一个反向迭代器,指向在递增字符串时向字符串开头移动的最后一个元素

关于算法:


如果您打算查找下一个回文数字,我不确定您实现的算法是否正确。例如,如果输入数字为123456,算法将检测到它不是回文,并将转换为12345_1_,该数字小于原始数字。

您试图使用重载which替换字符串中的一个字符。如果您看到字符串的成员函数,则尝试使用的replace的特定重载需要替换的字符数。因此,您应该将代码更改为:

testcases[i].replace(riter, riter, 1, *iter);

您试图使用替换字符串中一个字符的重载。如果您看到字符串的成员函数,则您试图使用的替换的特定重载需要替换的字符数。因此,您应该将代码更改为:

testcases[i].replace(riter, riter, 1, *iter);

除了dribeas的回答,我建议您将
riter
初始化为“
.end()-1
”,以避免过度索引字符串。

除了dribeas的回答,我建议您将
riter
初始化为“
.end()-1
”为了避免过度索引字符串。

您似乎遇到的问题是replace(…)的第一个参数需要是一个无符号整数,并且您的给定是一个字符串迭代器。您是否尝试在该字符串迭代器之前添加*以获取迭代器的内容?

您似乎遇到的问题是replace(…)的第一个参数需要是一个无符号的int,并且您提供了一个字符串迭代器。您是否尝试在该字符串迭代器之前添加*以获取迭代器的内容?

@dribeas-感谢您对我的代码提出的建议和分析。您在各个方面都是对的-我将改进您发布的代码,并发布改进的-(希望能奏效)稍后版本。我只需要开始检查这个字符串,不是从开始检查,而是从中间检查,如果没有您的建议,我不会注意到。@dribeas-谢谢您对我的代码提出建议和分析。您在各个方面都是对的-我将改进您发布的代码,并发布改进后的-(希望能工作)以后的版本。我只需要开始检查这个字符串,不是从一开始,而是从中间,但如果没有你的建议,我不会注意到。一开始我想知道你是否在谈论莎拉·佩林。一开始我想知道你是否在谈论莎拉·佩林。