So i'm trying to solve one problem about finding a longest unique sequence in array (vector a) and i've got a strange situation with iterators. If a see non-unique element i find iterator of the same element in the list of already found unique elements and then erase everything left to this element and the element itselft.
auto j = l.end(); // "l" is a list of integers
for(j = l.end(); j != l.begin(); --j) {
if(a[i] == *j) // i find a non-unique element in list
break;
}
j++;
for(auto it = l.begin(); it != j; ++it) {
ch.erase(*it); // ch is a map
res--;
}
l.erase(l.begin(),j);
So when i find a[i] in a map i go through this block of code and it works in 90% of cases but sometimes it just wont! Sometimes it's just depends on the size of the array, for example it works if the size is 300 and doesn't if the size is 7 however the data is same (at the time of running this block). In case of 7 elements it erases all the elements or erases none, in case 300 it works properly and erases only items before unique and unique itself. This is strange because i don't even use the size of array somewhere except looping array. Full code - https://pastebin.com/6yveBtL6 Test cases: 7 2 10 5 3 6 5 8 8 4 5 8 6 7 4 7 10 and choose different size. Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire