template<class Container, class Predicate> inline bool exists_and_only(const Container& cont, Predicate pred) { typename Container::iterator i = find_if(cont, pred); if (i == cont.end()) return false; if (find_if(++i, cont.end(), pred) != cont.end()) return false; return true; }
Could this be more generic by passing in the number of occurances?
template<typename InputIterator, typename T> inline bool exact_count(InputIterator first, InputIterator last, T const&, int count = 1); template<typename InputIterator, typename Predicate> inline bool exact_count_if(InputIterator first, InputIterator last, Predicate pred, int count = 1);
<hrule>
I don't think you need an extra algorithm to perform what count(...)==1 (or count(...)==count) already does with the same complexity (in particular, the same number of calls to operator== or to pred).
-- [Herve Bronnimann]