[英]Debug Assertion Failed String subscript out of range linked list
我没有为输入的5x ^ 7的链表分配值。
链表节点值; nodeValue1 = 5,nodeValue2 = x,nodeValue3 = 7。
表达式:字符串下标超出范围行:1441
功能:链表写
template <typename T>
void write(node<T>*front, const string &seperator="---->"){
node <T>*curr;
curr=front;
while(curr!=NULL){
cout<<curr->nodeValue1<<" "<<curr->nodeValue2<<" "<<curr->nodeValue3;
curr=curr->next;
if(curr!=NULL)
cout<<" "<<seperator;
}
}
Aktar函数//分配多项式链表。
void Aktar(string s){
int i=0,katsayi,derece;
string kat,us;
node <char>*front=NULL,*newNode;
while(s[i]!='\0'){
while(s[i]!='x'){
kat+=s[i];
i++;
}
if(s[i]=='x' && s[i+1]!='^')
us='1';
i++;//^ geçmesi için
while(s[i]=='^'){
us+=s[i+1];
i++;
if(s[i]=='-' || s[i]=='+')
break;
}
katsayi=atoi(&kat[0]);
derece=atoi(&us[0]);
newNode=new node<char>(katsayi,'x',derece,front);
front=newNode;
katsayi=0;
derece=0;
kat=' ';
us=' ';
}
write(front);
}
//主要
int main() {
string polinom;
cout << "Enter the polynomial:";
cin >>polinom;
Aktar(polinom);
return 0;
}
首先,你不应该用来对付比较'\\0'
为std::string
。 对象碰巧是终止为null的对象,但这主要是允许与C API进行交互的黑客。 将索引与s.size()
进行比较或使用迭代器进行旅行会更好。
您的实际问题可能是第嵌套循环(实际上,第二嵌套循环具有相同的问题),试图找到一个'x'
字符:一旦外while
输入,代码一直寻找一个'x'
不用理字符串的大小。 最终将触发调试异常,例如,当i == s.size()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.