CPP   75
iterator where
Guest on 3rd February 2023 01:39:57 AM


  1. #include <iostream>
  2. #include <cassert>
  3. #include <list>
  4. #include <algorithm> // for find
  5. using namespace std;
  6. template <typename Container>
  7. Container make(const char s[])
  8. {
  9.   return Container(&s[0], &s[strlen(s)]);
  10. }
  11.  
  12. int main()
  13. {
  14.   cout << "Demonstrating generic find algorithm with "
  15.        << "a list." << endl;
  16.  
  17.   list<char> list1 = make< list<char> >("C++ is a better C");
  18.  
  19.   // Search for the first occurrence of the letter e:
  20.   list<char>::iterator
  21.     where = find(list1.begin(), list1.end(), 'e');
  22.  
  23.   list<char>::iterator next = where;
  24.   ++next;
  25.   assert (*where == 'e' && *next == 't');
  26.   cout << " --- Ok." << endl;
  27.   return 0;
  28. }

Raw Paste

Login or Register to edit or fork this paste. It's free.