繁体   English   中英

映射/设置迭代器不可解除(C ++)-调试断言失败?

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

我有一个程序,必须使用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));
        }
    }
}
auto x = max_element(m.begin(), m.end(), m.value_comp());

这里m是空的,因此x将是您尝试遵循的end 可能是您忘记了调用countRepetition()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM