//C语言 int linearSearch(int *arr,int len,int key) { if(arr == NULL || len <= 0) return -1; for(int i = 0;i < len;++i) { if(key == arr[i]) return i; } return -1; }
//C++ const size_t arraySize = 100;//数组长度 template<typename T> size_t linearSearch(const array<T, arraySize> &arr, const T key) { int i = 0; while (i < arr.size() && arr[i] != key) ++i; if (i == arr.size()) return -1; else return i; }