Iterator not dereferncable

0

Wywala mi taki błąd jak w tytule tematu, podczas printowania:

#include <iostream>
#include <vector>
#include <map>
using namespace std;
int main()
{
	ios_base::sync_with_stdio(0);
	int t, prod, il;
	map<unsigned, unsigned> lista;
	vector< map<unsigned, unsigned>::iterator > vect;
	map<unsigned, unsigned>::iterator it;
	scanf("%d", &t);
	vect.reserve(t);
	for(int i = 0; i < t; i++)
	{
		scanf("%d %d", &prod, &il);
		if(it = lista.find(prod), it != lista.end())
			lista[prod] += il;
		else
		{
			lista[prod] = il;
			vect.push_back(it);
		}
	}
	printf("%d\n", vect.size());
	for(vector< map<unsigned, unsigned>::iterator >::iterator i = vect.begin(); i != vect.end(); i++)
		printf("%d %d\n", (*i)->first, (*i)->second);

	system("PAUSE");
	return 0;
}
0

a co niby ma robić ten "if(it = lista.find(prod), it != lista.end())"? Czy ty wiesz w ogóle co w tym miejscu robi przecinek?
twoją intencją pewnie było: if( (it = lista.find(prod)) != lista.end())

Tą mapą iteratowów, to już mnie zupełnie dobijasz.

0

Powiem szczerze ze nie mam pojęcia jaki ten przecinek ma wpływ na działanie. Poprawiłem, jednak błąd nadal się pojawia.

0

Może zrób to tak (według zasady KISS):

#include <iostream>
#include <vector>
#include <map>

using namespace std;

typedef map<unsigned, unsigned> IndeksPoduktow;
typedef vector<unsigned> ListaPoduktow;


int main()
{
        ios_base::sync_with_stdio(0);
        int t, prod, il;
        IndeksPoduktow indeks;
        ListaPoduktow vect;
        IndeksPoduktow ::iterator it;

        cin >> t;
        vect.reserve(t);

        for(int i = 0; i < t; i++)
        {
                cin >> prod >> il;
                it = indeks.find(prod)
                if(it != indeks.end())
                        it->second += il;
                else
                {
                        indeks[prod] = il;
                        vect.push_back(it);
                }
        }
        
        cout << vect.size();
        for(ListaPoduktow::iterator >::iterator i = vect.begin(); i != vect.end(); ++i)
                cout << (*i) << " , "<<indeks[*i] << endl;

/// albo alternatywnie bez wektora:
        for(IndeksPoduktow::iterator >::iterator i = indeks.begin(); i != indeks.end(); ++i)
                cout << i->first << " "<< i->second << endl;

        system("PAUSE");
        return 0;
}
0

Dzięki za odpowiedź. Już zaradziłem problemowi.

1 użytkowników online, w tym zalogowanych: 0, gości: 1