Mega Code Archive

 
Categories / C++ / Function
 

Find all template function

#include <vector> #include <iostream> #include <functional> #include <algorithm> using namespace std; template <typename InputIterator, typename Predicate> vector<InputIterator> find_all(InputIterator first, InputIterator last, Predicate pred) {   vector<InputIterator> res;   while (true) {     first = find_if(first, last, pred);     if (first == last) {       break;     }     res.push_back(first);     ++first;   }   return (res); } int main(int argc, char** argv){   int arr[] = {3, 4, 5, 4, 5, 6, 5, 8};   vector<int*> all = find_all(arr, arr + 8, bind2nd(equal_to<int>(), 5));       cout << "Found " << all.size() << " matching elements: ";      for (vector<int*>::iterator it = all.begin(); it != all.end(); ++it) {     cout << **it << " ";   }   return (0); }