首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

string能否作为函数的形参? 出错:std:out_of_range 和 Run-Time Check Failure #0,该怎么解决

2012-02-04 
string能否作为函数的形参? 出错:std::out_of_range 和 Run-Time Check Failure #0环境是VS2010出错提示有

string能否作为函数的形参? 出错:std::out_of_range 和 Run-Time Check Failure #0
环境是VS2010

出错提示有以下几类:

BoyerMoore.exe 中的 0x74ff9673 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0036f1f4 处的 std::out_of_range。

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

BoyerMoore.exe 中的 0x74ff9673 处有未经处理的异常: Microsoft C++ 异常: 内存位置 0x0036f1f4 处的 std::out_of_range。

HEAP CORRUPTION DETECTED: after Normal block(#137) at 0x
CRT detected that the application wrote to memory after end of heap buffer

最后是代码,代码是要实现BoyerMoore算法

C/C++ code
//BM#include <cstdio>#include <iostream>#include <string>//#pragma comment(lib,"**.lib")using namespace std;string cShiftTable(string mode, string text);string cSuffTable(string mode);int BM(string mode, string text);int main(){    int location = -1;        string mode, text;    cout << "请输入模式字符串:" << endl;    getline(cin, mode);    cout << "请输入文本:" << endl;    getline(cin, text);    location = BM(mode, text);    if(location == -1){        cout << "模式在文本中不存在!" << endl;    }    else {        cout << "模式在文本中的位置为:" + location << endl;    }    return 0;}//int BM(const char * a, const char * b){int BM(string mode, string text){    //string mode(a), text(b);    int m = mode.length(), t = text.length();    int d = 0, d1 = 0, d2 = 0;    int PText = 0, PMode = 0, MatchCount = 0;    string ShiftTable, SuffTable;    ShiftTable = cShiftTable(mode, text);             //生成坏符号移动表    SuffTable = cSuffTable(mode);                     //生成好后缀移动表    while(PText < t-m){        PMode = m-1;        while( (PMode >= 0) && mode.at(PMode) == text.at(PText+PMode)){            try {                mode.at(PMode);                  text.at(PText+PMode);            }              catch (std::out_of_range &exc) {                  std::cerr << exc.what() << " Line:" << __LINE__ << " File:" << __FILE__ << endl;              }              MatchCount += 1;            PMode--;        }        if(MatchCount == m){            return PText;        }        else{            try {                ShiftTable.at( mode.at(m-MatchCount-1));                  SuffTable.at(MatchCount);            }              catch (std::out_of_range &exc) {                  std::cerr << exc.what() << " Line:" << __LINE__ << " File:" << __FILE__ << endl;              }             d1 = max( int(ShiftTable.at( mode.at(m-MatchCount-1))) - MatchCount, 1);            d2 = SuffTable.at(MatchCount);            d = max(d1, d2);            PText += d;        }    }    return -1;}string cShiftTable(string mode,string text){    int i = 0, Max = 0;    int m = mode.length();    int t = text.length();    for(i = 0;i < m-1;i++){        try {            mode.at(i);        }          catch (std::out_of_range &exc) {              std::cerr << exc.what() << " Line:" << __LINE__ << " File:" << __FILE__ << endl;          }        if(mode.at(i) < mode.at(i+1))  Max = i+1;    }    string ShiftTable(int (mode.at(Max)), m);    for(i = 0;i < m;i++){        ShiftTable.at( mode.at(i)) = m-i-1;    }    return ShiftTable;}string cSuffTable(string mode){    int i = 0, j = 0;    int m = mode.length();    string SuffTable(m-1, m);    for(i = 1;i < m;i++){        string Suff(mode, m-i, i);        for(j = m-i-i;j > i-1;j--){            string Compare(mode, j, i);            if(Compare == Suff){                SuffTable.at(i) = m-j-i;                        }        }    }    return SuffTable;} 




搞不明白是哪里出了问题,好像是string类型不能作形参?还是我用的方法不对?

我逐步运行,跑到 int BM(string mode, string text) 这里就不断地进入很多我看不懂的文件,是不是在这里出了什么问题?

请多指教,谢谢!



[解决办法]
for(i = 0;i < m;i++){
ShiftTable.at( mode.at(i)) = m-i-1;
}
越界了
[解决办法]
ShiftTable.at( mode.at(i)) = m-i-1;
你这句代码是要干嘛。
string的at方法返回的是指定位置的字符。

mode是模式字符串,那么你用mode.at(i)返回的是i位置的字符。

而你又用ShiftTable.at(mode.at(i)),这里明显有逻辑错误,
如果mode.at(i)得到的字符的编码值大于ShiftTable的长度,那么你这句话就会内存访问越界。

还有,代码写的太乱。

热点排行