Remove struct struct element from std :: vector

I have a struct element vector as shown below:

struct pnt
{
    bool has;
    int num;
};
std::vector<pnt> myvector;

let it have a vector sample, for example:

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 find duplicated elements (in terms of having the same number) and delete the one that has the bool false element. so my vector will become like this:

myvector (num): 3 4 3 5 7 8 9 10                                                         
myvector (has): 1 1 1 1 0 0 0 1 

I am writing the 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 this by consistently checking members. but the real vector can be very long. so I first started looking for a member with a true Boolean, then I checked the next and previous member. The question is how can I modify the above code in terms of efficiency and reliability.

. ++ 03 ( ++ 11). boos ( 1.53), , , - .:)

+4
2

:

  • num, has true set<int>
  • vector<pnt> , has false num set<int>

:

struct filter {
    set<int> seen;
    bool operator()(const pnt& p) {
        return !p.has && (seen.find(p.num) != seen.end());
    }
};
...
filter f;
for (vector<pnt>::const_iterator i = v.begin() ; i != v.end() ; i++) {
    if (i->has) {
        f.seen.insert(i->num);
    }
}
v.erase(remove_if(v.begin(), v.end(), f), v.end());

-

+2

std:: sort std:: unique :

[](const pnt& a, const pnt& b) { return a.num < b.num; }

Boost Range :

++ 03 Live On Coliru

#include <boost/range.hpp>
#include <boost/range/algorithm.hpp>
#include <iostream>

using namespace boost;

struct pnt {
    int num;
    bool has;

    pnt(int num = 0, bool has = false) : num(num), has(has) {}

    friend bool operator<(pnt const& a, pnt const& b) { return a.num<b.num; }
    friend bool operator==(pnt const& a, pnt const& b) { return a.num==b.num; }
};

int main() {
    std::vector<pnt> v { {10,0 },{10,1 },{9,0 },{8,0 },{7,0 },{5,1 },{5,0 },{3,1 },{4,0 },{4,1 },{3,1 } };

    for (pnt p : boost::unique(boost::sort(v)))
        std::cout << "{ num:" << p.num << ", has:" << p.has << "}\n";
}

, , ,

it = std::find(v.begin(), v.end(), 3); // find a `pnt` with `num==3`

+1