JaronZou 2022-04-25 18:15 采纳率: 100%
浏览 60
已结题

cpp map 插入时报错

这段代码编译通过,但是执行出问题,所以我就打了断点,发现代码跑到 // 1 这个地方就挂了。
这明明就是一句普通的map插入操作啊,怎么会出错呢,希望哪位能帮忙解答,不胜感激。
另外:// 1 处的代码我改为使用 insert 进行插入也挂了

struct greaterKey{
    bool operator()(const map<string, int>::iterator &key1, 
                    const map<string, int>::iterator &key2) {
        return key1->second > key2->second;
    }
};

vector<string> getAppearMostK(const vector<string> &sarr, int k) {
    // 建立词频表
    map<string, int> wordTable;
    for(int i = 0; i < sarr.size(); ++i) {
        if(wordTable.count(sarr[i]) == 0) {
            wordTable[sarr[i]] = 0;           //   1
        }
        else{
            ++wordTable[sarr[i]];
        }
    }
    // 创建小根堆,以map<string, int>::iterator->second比较
    priority_queue<map<string, int>::iterator, 
                   vector<map<string, int>::iterator>,
                   greaterKey> smallQue;
    // k个元素入队
    int count = 0;
    auto beg = wordTable.begin();
    for(; beg != wordTable.end(); ++beg) {
        smallQue.push(beg);
        ++count;
        if(count == k) {
            break;
        }
    }
    // 剩下的元素入队一个,弹出一个,保证小根堆中存储的元素数量为K
    // 最终遍历完毕,得到最大的k个元素
    while(beg != wordTable.end()) {
        ++beg;
        smallQue.push(beg);
        smallQue.pop();
    }
    // 将结果写入ans
    vector<string> ans;
    while(!smallQue.empty()) {
        ans.push_back(smallQue.top()->first);
        smallQue.pop();
    }
    return ans;
}

gdb 调试的截图如下:

img

  • 写回答

2条回答 默认 最新

  • JaronZou 2022-04-26 09:53
    关注

    问题已解决,错误的原因在于 调用 getAppearMostK() 时调用的参数出错,传了一个空的数组进去,
    第二点是35行的 beg 应该先 push 再 ++,传参导致的问题纯属眼瞎,抱歉耽误大家时间了,完整版的代码如下:

    #include <vector>
    #include <string>
    #include <queue>
    #include <map>
    #include <iostream>
    
    using namespace std;
    
    // 小根堆的比较函数
    struct greaterKey{
        bool operator()(const map<string, int>::iterator &key1, 
                        const map<string, int>::iterator &key2) {
            return key1->second > key2->second;
        }
    };
    
    vector<string> getAppearMostK(const vector<string> &sarr, int k) {
        // 建立词频表
        map<string, int> wordTable;
        for(int i = 0; i < sarr.size(); ++i) {
            if(wordTable.count(sarr[i]) == 0) {
                wordTable[sarr[i]] = 0;
            }
            else{
                ++wordTable[sarr[i]];
            }
        }
        // 创建小根堆,以map<string, int>::iterator->second比较
        priority_queue<map<string, int>::iterator, 
                       vector<map<string, int>::iterator>,
                       greaterKey> smallQue;
        // k个元素入队
        auto beg = wordTable.begin();
        for (int i = 0; beg != wordTable.end() && i < k; ++beg, ++i) {
            smallQue.push(beg);
        }
        // 剩下的元素入队一个,弹出一个,保证小根堆中存储的元素数量为K
        // 最终遍历完毕,得到最大的k个元素
        while(beg != wordTable.end()) {
            smallQue.push(beg);
            smallQue.pop();
            ++beg;
        }
        // 将结果写入ans
        vector<string> ans;
        while(!smallQue.empty()) {
            ans.push_back(smallQue.top()->first);
            smallQue.pop();
        }
        return ans;
    }
    
    int main() {
        vector<string> sarr = {
            "sdad", "sdad", "sdad", "dgf", "sdads", "dgf",
            "dgf", "ghgh", "ghgh", "sdasd", "sdasds", "gfgcb", "scxc"
            "sdasddgf", "xvxcv", "wrer", "bgb", "aqd"
        };
        vector<string> ans = getAppearMostK(sarr, 3);
        for(int i = 0; i < 3; ++i) {
            cout << ans[i] << " ";
        }
        cout << endl;
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月6日
  • 已采纳回答 4月28日
  • 请采纳用户回复 4月28日
  • 修改了问题 4月25日
  • 展开全部

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统