[英]Unresolved Segmentation fault while trying to simplify directory path using stacks
當我使用堆棧簡化目錄路徑時,我試圖解決分段錯誤。 但是我似乎找不到任何會導致問題的原因。 我想念什么嗎?
string Solution::simplifyPath(string A) {
string a="";
stack<char> one;
stack<char> res;
for(int i=0; i<A.length();i++){
one.push(A[i]);
}
one.pop();
while(one.top()!='/'){
res.push(one.top());
one.pop();
}
res.push('/');
while(res.top()!=NULL){
a+=res.top();
res.pop();
}
return a;
}
Error message:
Runtime Error. Your submission stopped because of a runtime error. ex: division by zero, array index out of bounds, uncaught exception You can try testing your code with custom input and try putting debug statements in your code.
Segmentation fault.
while(res.top()!=NULL)
當堆棧為空時, std::stack::top()
不返回NULL。 相反,它將導致未定義的行為。
在使用stack::top()
或stack::pop()
之前,您必須檢查堆棧是否為空。
while(!one.empty() && one.top()!='/')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.