Hi guys, I have a std::list of numbers, and I'd like to remove any numbers that are repeated twice. I can assume all lists passed into the function will be in ascending order and as such all repeats will be next to each other (e.g. <1,2,3,3,4,5> etc.).
We're also to find the product of any removed numbers (for example, a list of <1,2,2,3,3,4,5> would return 6 (2*3)).
Here is my code:
Code:
#include <list>
#include <algorithm>
#include "p6.h"
unsigned p6(std::list<unsigned> & factors, const unsigned repeats){
int repeatedValue;
int product=1;
std::list<unsigned>::iterator it;
it = std::adjacent_find (factors.begin(), factors.end());
if (it!=factors.end()){
repeatedValue=*it;
it = factors.erase(it);
it = factors.erase(it);
}
product = product*repeatedValue;
return product;
}
My problem is getting the program to perform the adjacent find more than once, any attempt to put it in a while loop gives me errors such as "list iterators incompatible" or "list iterator is not deferenceable."
Can anybody explain to me what these errors mean and/or why I'm getting them? I feel like there's something about iterators I'm not understanding.
I don't like coding in Java to start off with (I feel like I'm asking if I can do stuff, instead of just doing stuff like in C++), putting all the Android stuff on top of that just made things worse. I also didn't like using the SDK, it likes to give weird errors for no reason, I found. When it works, it's great, but I just find myself fighting against more often than not.