// This file contains sample implementations of assorted algorithms // from the standard library. It is not actually used by any of the // other code in the book. template In find(In begin, In end, const X& x) { while (begin != end && *begin != x) ++begin; return begin; } template Out copy(In begin, In end, Out dest) { while (begin != end) *dest++ = *begin++; return dest; } template void replace(For beg, For end, const X& x, const X& y) { while (beg != end) { if (*beg == x) *beg = y; ++beg; } } template void reverse(Bi begin, Bi end) { while (begin != end) { --end; if (begin != end) swap(*begin++, *end); } } template bool binary_search(Ran begin, Ran end, const X& x) { while (begin < end) { // find the midpoint of the range Ran mid = begin + (end - begin) / 2; // see which part of the range contains `x'; keep looking only in that part if (x < *mid) end = mid; else if (*mid < x) begin = mid + 1; // if we got here, then `*mid == x' so we're done else return true; } return false; }