Map / set iterator not dereferencable(C ++) - Debug Assertion Failed?

时间:2018-04-18 04:41:35

标签: c++ dictionary compiler-errors stl

我有一个程序,我必须使用STL中的地图。然后,我使用auto关键字(或者map <int><int>::iterator)从地图容器中获取最大元素。但是,编译器抛出错误Debug Assertion Failed,我无法弄清楚为什么会发生这种情况?

// List.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <list>
#include <map>
using namespace std;
char number;
list <char> l;
map <int,int> m;
list <char>::iterator START;
void countRepetition();
int main()
{
    do {
        number = getchar();
        if (number != '0') {
            l.push_back(number);
        }
    } while (number != '0');

    /*for (START = l.begin(); START != l.end(); START++) {
        m.push_back(countRepetition(*START));
    }

    for (int i = 0; i < m.size(); i++) {
        cout << m[i] << endl;
    }
    */
    countRepetition();
    auto x = max_element(m.begin(), m.end(), m.value_comp());
    cout << x->second << endl;
    return 0;
}
void countRepetition() {
    for (auto i = l.begin(); i != l.end(); i++) {
        int counter = 0;
        for (auto j = l.begin(); j != l.end(); j++) {
            if (*i == *j) {
                counter++;
            }
            m.insert(make_pair(counter,*i));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

auto x = max_element(m.begin(), m.end(), m.value_comp());

此处m为空,因此x将是end您尝试顺从的countRepetition()。 可能忘了给TrdInstrmtLegGrp打电话。