Review of the STL source analysis, the first few simple algorithms to achieve their own. The parameter does not use an iterator, directly using an integer array.
#include <cstdlib> #include <cstdio>/* applies to ordered intervals, judging whether the elements in B are all contained in a. The default is ascending, if descending, change the comparison symbol */BOOL includes (int* A, size_t al, int *b, size_t bl) {size_t i = 0, j = 0; while (I < Al && Amp J < BL) {if (A[i] > B[j]) return false, else if (A[i] < b[j]) i++; else i++, j + +;} return j = = BL; }/* Place the sequence A in the previous segment with the pred condition as true, False in the back segment */int* partition (int* A, size_t begin, size_t end, predicate pred) {size_t i = Begin, J = end; while (true) {while (true) {if (i = = j) return I; else if (pred (A[i])) i++; else break;} while (true) {if (i = = j) ret Urn i; else if (!pred (A[j])) j--; else break; }//swap a[i] = A[i] + a[j]; A[J] = A[i]-a[j]; A[i] = A[i]-a[j]; i++; }}/* Overall exchange a[begin, middle-1] and A[middle, end-1] */inline void rotate (int* A, size_t begin, size_t middle, size_t end) { for (size_t i = middle;;) {//swap int temp = a[i]; A[i] = A[begin]; A[begin] = temp; i++, begin++; if (begin = = middle) {if (i = = end) return; MI Ddle = i; } else if (i = = end) i = middle; } */* in A[B1, e1-1] to find the first occurrence of B[B2, e2-1], failed to return e1 */int search (int* A, size_t B1, size_t E1, int* B, size_t B2, size_t E2 {size_t D1 = e1-b1; size_t d2 = e2-b2; if (D1 < D2) return e1; size_t i = b1, j = B2; while (j! = E2) {if (A[i] = = B[j]) i++, j + +; else if (D1 = = D2) return E1; else {i = ++b1; j = B2; d1--;}} return B1; } int main () {int a[] = {1, 2, 3, 4, 5}; int b[] = {1, 3, 5}, if (includes (A, sizeof (a)/sizeof (int), B, sizeof (b)/siz EOF (int))) printf ("yes/n"); int c[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Rotate (c, 0, (sizeof (c)/sizeof (int))/2, sizeof (c)/sizeof (int)); for (size_t i = 0; i < sizeof (c)/sizeof (int); i++) printf ("%d", c[i]); printf ("/n"); int d[] = {1, 2, 3, 4, 5}; int e[] = {7, 5}; printf ("%d/n", search (d, 0, sizeof (d)/sizeof (int), E, 0, sizeof (e)/sizeof (int))); return 0; }