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算法
//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;}