想打球的码农 2024-06-03 00:13 采纳率: 66.7%
浏览 8
已结题

C++代码报错原因求解决


class Solution {
public:
    string clearStars(string s) {
        priority_queue<char,vector<char>,greater<char>>pq;
        stack<char>s1;
        stack<char>s2;
        for(int i=0;i<s.size();i++){
            if(s[i]!='*'){
                pq.push(s[i]);
                s1.push(s[i]);
            }else{
                while(s1.top()!=pq.top()){
                    s2.push(s1.top());
                    s1.pop();
                }
                pq.pop();
                s1.pop();
                while(!s2.empty()){
                    s1.push(s2.top());
                    s2.pop();
                }
            }
        }
        string ans="";
        while(!s1.empty()){
            ans+=s1.top();
            s1.pop();
        }
        reverse(ans.begin(),ans.end());
        
        
        return 0;
    }
};

报错:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid

  • 写回答

3条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-03 00:13
    关注

    引自免费微信小程序:皆我百晓生

    在你的C++代码中,clearStars 函数返回了一个整数 0 而不是字符串。这与函数签名中的返回类型(string)不符,导致编译错误。你需要将返回值从 0 改为 ans

    这里是修改后的代码:

    #include <queue>
    #include <stack>
    #include <string>
    #include <algorithm>
    
    class Solution {
    public:
        std::string clearStars(std::string s) {
            std::priority_queue<char, std::vector<char>, std::greater<char>> pq;
            std::stack<char> s1;
            std::stack<char> s2;
            for (int i = 0; i < s.size(); i++) {
                if (s[i] != '*') {
                    pq.push(s[i]);
                    s1.push(s[i]);
                } else {
                    while (!s1.empty() && s1.top() != pq.top()) {
                        s2.push(s1.top());
                        s1.pop();
                    }
                    pq.pop();
                    s1.pop();
                    while (!s2.empty()) {
                        s1.push(s2.top());
                        s2.pop();
                    }
                }
            }
            std::string ans = "";
            while (!s1.empty()) {
                ans += s1.top();
                s1.pop();
            }
            std::reverse(ans.begin(), ans.end());
    
            return ans;
        }
    };
    
    // 使用示例
    int main() {
        Solution solution;
        std::string input = "*a*b*c*d*e";
        std::string result = solution.clearStars(input);
        std::cout << "Result: " << result << std::endl;
        return 0;
    }
    

    这段代码修复了返回类型的错误,并添加了必要的头文件包含。现在这个函数应该能正确地清除字符串中的星号并返回结果。如果你运行这个程序,它会输出 "abcde"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月8日
  • 已采纳回答 6月3日
  • 创建了问题 6月3日

悬赏问题

  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥35 跳过我的世界插件ip验证
  • ¥15 抖音看过的视频,缓存在哪个文件
  • ¥15 自定义损失函数报输入参数的数目不足
  • ¥15 如果我想学习C大家有是的的资料吗
  • ¥15 根据文件名称对文件进行排序
  • ¥15 deploylinux的ubuntu系统无法成功安装使用MySQL❓