I have a vector of struct member like below:
struct pnt
{
bool has;
int num;
};
std::vector<pnt> myvector;
let have a sample vector like:
myvector (num): 3 4 4 3 5 5 7 8 9 10 10
myvector (has): 1 1 0 1 0 1 0 0 0 1 0
What I want to do is to find duplicated members (in terms of having same int num) and remove the one with false bool member. so that my vector become like this:
myvector (num): 3 4 3 5 7 8 9 10
myvector (has): 1 1 1 1 0 0 0 1
to do so, I write following function:
void removeDuplicatedPnt(pnt_vec& myvector)
{
std::vector<pnt>::iterator pnt_iter;
for( pnt_iter = myvector.begin(); pnt_iter != myvector.end(); ++pnt_iter)
{
if(pnt_iter->has)
{
if(pnt_iter->num == (pnt_iter+1)->num)
{
myvector.erase(pnt_iter+1);
}
if(pnt_iter == myvector.begin())
{
continue;
}
if(pnt_iter->num == (pnt_iter-1)->num)
{
myvector.erase(pnt_iter-1);
pnt_iter++;
}
}
}
}
I could also do it by sequential checking of members. but the real vector could be very long. so that is why first I went to find the member with true boolean then I checked the the next and previous member. The question is that how I can modify above code in terms of efficiency and robustness.
NOTE: I can only use C++03 (not C++11). I can also use boos (version 1.53), so feel free if think there is any useful function there. :)