#include <iterator>
#include <algorithm>
template<typename Iterator, typename Lambda, typename Counter>
Iterator find_if_nth( Iterator begin, Iterator end, Lambda closure, Counter n ) {
typedef typename std::iterator_traits<Iterator>::reference Tref;
return std::find_if(begin, end, [&n,&closure](Tref x) {
return closure(x) && !(--n);
});
}
#include <vector>
#include <iostream>
int main() {
std::vector<int> v{1,2,3,1,2,3,1,2,3};
auto it = find_if_nth( v.begin(), v.end(), [](int x){return x==2;}, 2);
std::cout << "2nd 2 is at offset " << (it-v.begin()) << " with value [" << *it << "]\n";
}
I2luY2x1ZGUgPGl0ZXJhdG9yPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKICAgIHRlbXBsYXRlPHR5cGVuYW1lIEl0ZXJhdG9yLCB0eXBlbmFtZSBMYW1iZGEsIHR5cGVuYW1lIENvdW50ZXI+CiAgICBJdGVyYXRvciBmaW5kX2lmX250aCggSXRlcmF0b3IgYmVnaW4sIEl0ZXJhdG9yIGVuZCwgTGFtYmRhIGNsb3N1cmUsIENvdW50ZXIgbiApIHsKICAgICAgdHlwZWRlZiB0eXBlbmFtZSBzdGQ6Oml0ZXJhdG9yX3RyYWl0czxJdGVyYXRvcj46OnJlZmVyZW5jZSBUcmVmOwogICAgICByZXR1cm4gc3RkOjpmaW5kX2lmKGJlZ2luLCBlbmQsIFsmbiwmY2xvc3VyZV0oVHJlZiB4KSB7CiAgICAgICAgcmV0dXJuIGNsb3N1cmUoeCkgJiYgISgtLW4pOwogICAgICB9KTsKICAgIH0KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgppbnQgbWFpbigpIHsKICAgIHN0ZDo6dmVjdG9yPGludD4gdnsxLDIsMywxLDIsMywxLDIsM307CiAgICBhdXRvIGl0ID0gZmluZF9pZl9udGgoIHYuYmVnaW4oKSwgdi5lbmQoKSwgW10oaW50IHgpe3JldHVybiB4PT0yO30sIDIpOwogICAgc3RkOjpjb3V0IDw8ICIybmQgMiBpcyBhdCBvZmZzZXQgIiA8PCAoaXQtdi5iZWdpbigpKSA8PCAiIHdpdGggdmFsdWUgWyIgPDwgKml0IDw8ICJdXG4iOwp9