I'm working with vectors and at some point there will be NULL entries; I want to erase all NULL occurrences within the given vectors. My approach so far is not working:
for(int i = sent_flit_list->size() - 1; i >= 0; i--)
if(sent_flit_list[i] == NULL)
sent_flit_list->erase(sent_flit_list[i]);
for(int i = sent_pkt_list->size() - 1; i >= 0; i--)
if(sent_pkt_list[i] == NULL)
sent_pkt_list->erase(sent_pkt_list[i]);
Where
vector<Flit*> *sent_flit_list;
vector<Packet*> *sent_pkt_list;
are the vectors. I have tried casting to a type (Flit*)NULL/(Flit*)0 but with no success.
Any help will be greatly appreciated.
vector::erase
takes an iterator, not a value, as a parameter. – Cleremove
for this rather than looping explicitly. I think the immediate cause of your problem is thatsent_flit_list
andsent_pkt_list
are pointers to vectors, not vectors, so that when you say e.g.sent_pkt_list[i]
that indexing operation isn't what you think it is. (This is not the only problem with the code, but it's the one that's causing that error message.) – Sheereestd::vector a
you iterate over the vector with indexing like you do, now erase value at position one, the 2 index is now wrong because the vector shrinked... Always use iterators (it
) and useit=a.erase(it)
– Damales