H'H
H'H

Reputation: 1678

remove copy struct member from std::vector

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. :)

Upvotes: 4

Views: 226

Answers (2)

sehe
sehe

Reputation: 393124

You can use std::sort and std::unique with a custom comparison predicate:

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

Here's a convenient way to demo it using Boost Range to reduce on typing:

Update C++03 version 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";
}

This actually has a few subtler points that make it possible to e.g. do

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

But that's only tangentially related

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

You can use this algorithm:

  • Collect all nums where has is true in a set<int>
  • Go through your vector<pnt> again, and remove all entries where has is false and num is present in the set<int>

Here is a sample implementation:

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());

Demo.

Upvotes: 2

Related Questions