fork 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::string v = "1 2 3 4 5 6 7 8";
  15. auto it = find_if_nth( v.begin(), v.end(), [](auto x){return x==' ';}, 2);
  16. std::cout << "2nd 2 is at offset " << (it-v.begin()) << " with value [" << *it << "]\n";
  17. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
2nd 2 is at offset 3 with value [ ]