I have a chunk of C++ code that is supposed to go through a sorted vector and delete reoccurring objects in-place. I completed the task (C1) using an iterator. I continued with the problem and wanted to do it using range-based for loop like the latter (C2). However, I ran into complier errors. Can someone explain why these two codes do not function the same and if there is a way of accessing the range-for-loop's internal iterator. C1)
for(auto itr = nums.begin(); itr != nums.end(); itr++) {
if(*itr != set) {
set = *itr; //sets the current loop number equal to a new number
} else {
nums.erase(itr--); //erases the number if it is repeating
}
}
C2)
for(auto num : nums) {
if(num != set) {
set = num;
} else {
nums.erase((&num));
}
}