fork(3) download
  1. #include <iterator>
  2. #include <algorithm>
  3.  
  4. template<typename Iterator, typename Lambda, typename Counter>
  5. Iterator find_if_nth( Iterator begin, Iterator end, Lambda closure, Counter n ) {
  6. typedef typename std::iterator_traits<Iterator>::reference Tref;
  7. return std::find_if(begin, end, [&n,&closure](Tref x) {
  8. return closure(x) && !(--n);
  9. });
  10. }
  11. #include <vector>
  12. #include <iostream>
  13. int main() {
  14. std::vector<int> v{1,2,3,1,2,3,1,2,3};
  15. auto it = find_if_nth( v.begin(), v.end(), [](int x){return x==2;}, 2);
  16. std::cout << "2nd 2 is at offset " << (it-v.begin()) << " with value [" << *it << "]\n";
  17. }
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
2nd 2 is at offset 4 with value [2]