Re: remove_if with a mask
On 8 juin, 11:36, Victor Bazarov <v.baza...@comcast.invalid> wrote:
On 6/8/2010 11:18 AM, Paul Bibbings wrote:
[..]
I threw together something similar to what you have and it *doesn't*
appear to be working.
16:15:35 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $cat vector_mask.cpp
// file: vector_mask.cpp
#include<vector>
#include<algorithm>
#include<iostream>
template<typename T>
class MaskFunctor {
public:
explicit MaskFunctor(std::vector<bool>& mask)
: mask_(mask)
, mask_iter_(mask_.begin())
{ }
bool operator()(const T&)
{
if (mask_iter_ == mask_.end())
return false;
else
return *mask_iter_++;
}
private:
std::vector<bool>& mask_;
std::vector<bool>::const_iterator mask_iter_;
};
int main()
{
std::vector<bool> mask;
for (int i = 0; i< 4; ++i)
{
mask.push_back(true);
mask.push_back(false);
}
std::vector<int> i_vec;
for (int i = 0; i< 8; ++i)
i_vec.push_back(i);
std::vector<int>::iterator end =
std::remove_if(i_vec.begin(), i_vec.end(), MaskFunc=
tor<int>(mask));
i_vec.erase(end, i_vec.end());
for (std::vector<int>::const_iterator ci = i_vec.begin=
();
ci != i_vec.end();
++ci)
{
std::cout<< *ci<< ' ';
}
std::cout<< '\n';
}
16:15:41 Paul Bibbings@JIJOU
/cygdrive/d/CPPProjects/CLCPP $./vector_mask
2 4 6
The implementation makes a copy of the predicate. It's important that
the predicate keeps the iterator by reference. Try rewriting it with
that in mind.
I tried :
template <class Z> struct maskChecker {
public:
maskChecker(const vector<bool> &mask) : it(mask.begin()) {}
bool operator()(Z &) const { return *it ++; }
private:
vector<bool>::const_iterator ⁢
};
template <class T> template<class Z>
void Selector<T>::compact(vector<Z> &data, const vector<bool> &mask) {
if (data.size() != mask.size()) throw UnmatchedSize(data.size(),
mask.size());
typename vector<Z>::iterator end = remove_if(data.begin(),
data.end(), maskChecker<Z>(mask));
data.erase(end, data.end());
}
This does not compile ...
I know this is about it being initialized by a temporary variable
returned by mask.begin() ..
Should I directly pass a reference to the iterator to the constructor
of maskChecker rather than the vector itself ?
V
--
I do not respond to top-posted replies, please don't ask