請幫助調試。它給我一個錯誤'字符串下標超出範圍錯誤'。C++字符串下標超出範圍
程序需要使用插入排序算法的文本進行排序。
下面是代碼:
#include<iostream>
#include<string>
using namespace std;
void insertionSort(string &text, int size) {
char temp;
int i;
for(int j=1;j<size;j++)
{
//text.push_back(temp);
temp=text[j];
i=j-1;
while(i>=0 && text[i]>temp)
{
text[i+1]=text[i];
i--;
}
text[i+1]=temp;
}
}
int main()
{
string text="this a just text need to be sorted";
int size = text.length();
insertionSort(text,size);
cout<<text<<endl;
return 0;
}
調試斷言失敗!
行:1441:
表達:串標超出範圍
我應該改變text[i+1]=text[j]
到text[i+1]=text[i]
;
是這功課嗎? – 2012-04-15 15:51:29
您的代碼格式不正確,而且您沒有指出代碼的哪一部分導致了問題。 – 2012-04-15 15:51:55
如果您指出錯誤發生的位置並使得格式更好,您可以使我們的生活更輕鬆。 – pmr 2012-04-15 15:52:02