<p>I have some function to find a value:</p>

<pre><code>struct FindPredicate
{

    FindPredicate(const SomeType&amp; t) : _t(t) {
    }
    bool operator()(SomeType&amp; t) {
      return t == _t;
    }

private:
    const SomeType&amp; _t;
};

bool ContainsValue(std::vector&lt;SomeType&gt;&amp; v, SomeType&amp; valueToFind) {
    return find_if(v.begin(), v.end(), FindPredicate(valueToFind)) != v.end();
}
</code></pre>

<p>Now I would like to write a function that checks if all members of a vector satisfy that predicate:</p>

<pre><code>bool AllSatisfy(std::vector&lt;SomeType&gt;&amp; v) {
    /* ... */
}
</code></pre>

<p>One solution is to use the <code>std::count_if</code> algorithm.</p>

<p>Does anyone know a solution that involves negating the predicate?</p>

The best solution is to use the
STL functional library
. By deriving your predicate from
```
unary_function<SomeType, bool>
```
, you'll then be able to use the
```
not1
```
function, which does precisely what you need (i.e. negating a unary predicate).
Here is how you could do that :
```
```
struct FindPredicate : public unary_function<SomeType, bool>
{
    FindPredicate(const SomeType& t) : _t(t) {}

    bool operator()(const SomeType& t) const {
      return t == _t;
    }

private:
    const SomeType& _t;
};

bool AllSatisfy(std::vector<SomeType>& v, SomeType& valueToFind)
{
    return find_if(v.begin(), 
                   v.end(), 
                   not1(FindPredicate(valueToFind))) == v.end();
}
```
```
If you want to roll your own solution (which is, IMHO, not the best option...), well, you could write another predicate that is the negation of the first one :
```
```
struct NotFindPredicate
{

    NotFindPredicate(const SomeType& t) : _t(t) {
    }
    bool operator()(SomeType& t) {
      return t != _t;
    }

private:
    const SomeType& _t;
};

bool AllSatisfy(std::vector<SomeType>& v) {
    return find_if(v.begin(), 
                   v.end(), 
                   NotFindPredicate(valueToFind)) == v.end();
}
```
```
Or you could do better and write a template functor negator, like :
```
```
template <class Functor>
struct Not
{
    Not(Functor & f) : func(f) {}

    template <typename ArgType>
    bool operator()(ArgType & arg) { return ! func(arg); }

  private:
    Functor & func;
};
```
```
that you could use as follow :
```
```
bool AllSatisfy(std::vector<SomeType>& v, SomeType& valueToFind)
{
    FindPredicate f(valueToFind);
    return find_if(v.begin(), v.end(), Not<FindPredicate>(f)) == v.end();
}
```
```
Of course, the latter solution is better because you can reuse the
Not
struct with every functor you want.