#include // provides the STL algorithms, including find, find_if, adjacent_find, find_first_of #include #include #include // provides less template function object #include #include using namespace std; // function object used in call to find_if - used for finding first element in a range < max class LessThan { public: LessThan(char max_arg): max(max_arg) {} bool operator() (char c) { return c < max; } private: char max; }; void main() { const unsigned int SIZE = 1000000; const char word[] = "find"; vector vc; vc.reserve(SIZE); srand(time(0)); for (vector::iterator v_iter = vc.begin(); v_iter != vc.begin() + SIZE; v_iter++) { vc.push_back(rand()%26 + 'a'); } // fill with random lower case letters cout << "First occurrence of 'z' at position " << find(vc.begin(), vc.end(), 'z') - vc.begin() << endl; cout << "First occurrence of two equal elements at position " << adjacent_find(vc.begin(), vc.end()) - vc.begin() << endl; cout << "First occurrence of one of " << word << " at position " << find_first_of(vc.begin(), vc.end(), &word[0], &word[strlen(word)]) - vc.begin() << endl; cout << "First occurrence of letter less than 'd' at position " << find_if(vc.begin(), vc.end(), LessThan('d')) - vc.begin() << endl; vector::const_iterator vc_iter = search(vc.begin(), vc.end(), &word[0], &word[strlen(word)]); if (vc_iter == vc.end()) { cout << "No occurrence of the word " << word << endl; } else { cout << "First occurrence of the word \"" << word << "\" at position " << vc_iter - vc.begin() << endl; vc_iter = find_end(vc.begin(), vc.end(), &word[0], &word[strlen(word)]); cout << "Last occurrence of the word \"" << word << "\" at position " << vc_iter - vc.begin() << endl; } cout << "First occurrence of 3 consecutive 'z's at position: " << search_n(vc.begin(), vc.end(), 3, 'z') - vc.begin() << endl; }