#include #include #include #include #include #include #include #include using namespace std; // convert an algorithm to be more generic (from Austern): // (1a) works with C-style strings // char * search (char * s, char c) { // while (*s != 0 && *s != c) { // ++s; // } // return (*s == c) ? s : 0; // } // (1b) works with (non-zero-terminated) character arrays // char * search (char * s, char * e, char c) { // while (s != e && *s != c) { // ++s; // } // return (*s == c) ? s : 0; // } // (1c) works with any reasonable iterator range // (what weakest concept is imposed?) template I search (I s, I e, T c) { while (s != e && *s != c) { ++s; } return s; } int main (int, char * []) { // (1a) search for input characters in a C-style string (provided) // char chararray [] = "hello, world!"; // (1b) search for input characters in a non-zero-terminated // character array // char chararray [] = {'h', 'e', 'l', 'l', 'o', ',', ' ', 'w', // 'o', 'r', 'l', 'd', '!'}; // (1c) search for input number in a list of integers list l; for (int i = 0; i <= 10; i+=2) { l.push_back(i); } int c = 0; do { cout << "Please type an integer: "; cin >> c; list::iterator position = search (l.begin(), l.end(), c); cout << endl << "Integer " << c; if (position != l.end()) { cout << " was found "; } else { cout << " was not found"; } cout << " in "; copy(l.begin(), l.end(), ostream_iterator(cout, " ")); cout << endl; } while (true); return 0; }