//* replace - iterators and standard algorithms *

#include <vector> #include <iostream> using namespace std; // this is an actual implementation of MAX_ELEMENT from <algorithm> template <typename iterator, typename type> void replace(iterator data_start, iterator data_end, const type &what, const type &with) { for (iterator scan=data_start; scan!=data_end; ++scan) if (*scan==what) *scan=with; } 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; } 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'); double wh, wi; cout << "Find what: "; cin >> wh; cout << "Replace with: "; cin >> wi; // do some testing replace(V.begin(), V.end(), wh, wi ); cout << "The altered data collection:" << endl; print_all(V.begin(), V.end()); return(0); }