调试断言失败 字符串下标超出范围
Debug Assertion Failed String subscript out of range
所以我写了一个简单的哈希类,当使用Visual Studio环境时,我收到调试断言错误"表达式:字符串下标超出范围" 但是,当我在命令提示符上使用c ++ Linker编译和运行时,程序运行良好。错误发生在 h.add("a", "la");
这是代码
#include <iostream>
#include <string>
using namespace std;
bool die(const string &msg);
class Hash{
public:
Hash( unsigned tablesize, unsigned maxUsed );
~Hash();
bool in( const string & code ) const;
bool getDescription( string & description, const string & code ) const;
void add( const string & code, const string & description );
void changeDescription( const string & code, const string & newDescription );
void showone(const string &code) const;
void show() const;
private:
struct Data{
string code;
string descrip;
};
unsigned hash(unsigned val) const;
unsigned rehash(unsigned val) const;
static unsigned Hash::partialHash( const string & code );
static bool prime( unsigned n );
unsigned findindex(const string &code) const;
Data *ptr;
unsigned maxUsed;
unsigned elements;
unsigned tablesize;
unsigned p;
unsigned p2;
};
Hash::Hash(unsigned size, unsigned maxused){
if(UINT_MAX-3<size || size<=4 || size<=maxused )
die("Invalid Constructor");
for(tablesize=size; !prime(tablesize); tablesize++){}
ptr=new Data[tablesize];
elements=0;
maxUsed=maxused;
for(p=tablesize; !prime(--p);){}
for(p2=p; !prime(--p2);){}
for(unsigned i=0; i<tablesize; i++){
ptr[i].code=" ";
ptr[i].descrip=" ";
}
}
Hash::~Hash(){
delete[] ptr;
}
bool Hash::in(const string &code)const{
if(code==ptr[findindex(code)].code)
return true;
return false;
}
void Hash::showone(const string &code) const{
unsigned i=findindex(code);
cout<<'['<<i<<"]: "<<ptr[i].code<<' '<<ptr[i].descrip<<'n';
}
void Hash::show() const{
for(unsigned i=0; i<tablesize; i++)
cout<<'['<<i<<"]: "<<ptr[i].code<<' '<<ptr[i].descrip<<'n';
}
bool Hash::getDescription( string & description, const string & code ) const{
if(in(code)){
description=ptr[findindex(code)].descrip;
return true;
}
return false;
}
void Hash::changeDescription(const string & code, const string & newdescription ){
if(in(code)){
ptr[findindex(code)].descrip=newdescription;
}else{
die("code not in table");
}
}
unsigned Hash::hash(unsigned partialHashValue)const{
return partialHashValue%p;
}
unsigned Hash::rehash(unsigned partialHashValue)const{
return partialHashValue%p2+1;
}
unsigned Hash::partialHash( const string & code ){
return (code[0]*26+code[1])*26+code[2];
}
void Hash::add( const string & code, const string & description ){
if(in(code)) die("can't add");
if(elements==maxUsed) die("Overflow");
unsigned i=findindex(code);
ptr[i].code=code;
ptr[i].descrip=description;
elements++;
}
bool Hash::prime( unsigned n ){
if( n < 4 ) return n > 1;
if( n%2 == 0 || n%3 == 0 ) return false;
for( unsigned fac = 5, inc = 4; ; fac += inc = 6-inc ){
if( fac > n/fac ) return true;
if( n%fac == 0 ) return false;
}
}
unsigned Hash::findindex( const string &code) const{
unsigned partial=partialHash(code);
unsigned hashnum = hash(partial);
if(ptr[hashnum].code==" " || ptr[hashnum].code == code) return hashnum;
unsigned rehashnum = rehash(partial);
do{
hashnum = (hashnum + rehashnum) % tablesize;
}while( ptr[hashnum].code != " " && ptr[hashnum].code != code );
return hashnum;
}
int main(){
Hash h(12, 8);
h.add("LAX", "Space Shuttle Endeavour arrived here 9/21/2012");
h.add("DEN", "jajaja");
h.add("gold", "lalalala");
h.add("Pp", "la");
h.add("a", "la");
h.add("b", "la");
h.add("c", "la");
h.add("d", "la");
cout<<"pn";
h.showone("LAX");
cout<<"nn";
h.show();
}
bool die(string const &msg){
cerr<<"fatal error: "<<msg;
exit(EXIT_FAILURE);
}
这是输出
[0]: gold lalalala
[1]: DEN jajaja
[2]: LAX Space Shuttle Endeavour arrived here 9/21/2012
[3]:
[4]:
[5]: a la
[6]: b la
[7]: Pp la
[8]:
[9]: d la
[10]: c la
[11]:
[12]:
在 partialHash 函件中,当代码只包含 "a" 时,code[2] 是边界外访问
unsigned Hash::partialHash( const string & code ){
return (code[0]*26+code[1])*26+code[2];
}
您可以使用 at() 函数并捕获抛出的异常
unsigned Hash::partialHash( const string & code )
{
try {
return (code.at(0)*26+code.at(1))*26+code.at(2);
}
catch(std::exception& e){
std::cout << e.what() << std::endl;
}
return 0;
}
相关文章:
- 尝试通过多个向量访问变量时,向量下标超出范围
- 矢量下标超出SFML游戏中的范围
- 运行时错误:矢量下标超出范围:正在检查空集
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- 编译期间矢量下标超出范围
- C++ 调试断言失败 - 矢量下标超出视觉工作室的范围
- 对矢量来说是全新的.矢量下标超出范围
- C++十进制到十六进制(字符串下标超出范围)
- 创建结构体向量,表达式:向量下标超出范围
- 读取矢量时矢量下标超出范围
- 断言失败错误,C++中的矢量下标超出范围问题
- 如何在 Linux 上强制执行矢量下标超出范围的调试断言
- 表达式:矢量下标超出范围
- 矢量下标超出范围,带有嵌套的 for 循环
- 向量下标超出向量向量的范围
- gcc8.2如何启用警告:数组下标在数组范围之上[-warray-bounds]
- 向量下标出的范围错误.即使向量的索引大于访问数据的索引,也会发生误差
- std::vector<std::vector<int>>:调试断言失败。C++矢量下标超出范围保留内存
- 重命名两个目录中的文件名,如果它们之间的某些字符匹配 - 矢量下标超出范围
- 行进立方体重建 - 向量下标超出范围