//* find adjacent - iterators and standard algorithms *

#include <vector> #include <iostream> using namespace std; template <typename iterator> iterator adjacent_find(iterator beg, iterator data_end) { iterator found = data_end; // nothing found - return the data_end value iterator if (beg != data_end) { // we cannot proceed with ++ahead if data collection is empty iterator ahead = beg; ++ahead; for (iterator behind = beg; ahead != data_end; ++ahead, ++behind) { if (*ahead == *behind) { found = behind; // OR found = ahead; - depends on what do you need break; } } } return(found); } template <typename iterator> void print_all(iterator data_start, iterator data_end) { for (iterator scan = data_start; scan != data_end; ++scan) cout << ' ' << *scan; cout << endl; } // sample test program int main() { // get a sample vector with some data vector<double> V; cout << "Please enter several values followed by the letter 's':" << endl; for (;;) { double x; cin >> x; if (cin.fail()) break; V.push_back(x); } cin.clear(); cin.ignore(256, '\n'); // do some testing vector<double>::iterator pos; pos = adjacent_find(V.begin(), V.end()); if (pos == V.end()) cout << "Adjacent equal values not found in this vector." << endl; else cout << "Adjacent equal values found! First pair equals " << *pos << endl; return(0); }