#include <iostream> using namespace std; //--------------------针对数组的,简单的查找------------------ /**/ template<typename elemtype> int searchArray(elemtype *array, elemtype e,int length)// const 去保数据不被修改 { //计算数组长度 for(int i =length -1;i>=0;i--) { if(array[i]==e) return i; } return -1; } void main() { // float array[6]={1.0,5.0,66.0,3.0,5.0,6.0}; int array[6] ={1,2,3,4,5,6}; int length =sizeof(&array)/sizeof(int); int position=-1; int e= 2;//需要查找的数据 position = searchArray(array,e,length); cout.width(5);//cout.width() 只对紧挨着的数据的输出有效 cout<<e<<" 数据在数组中的位置 : "; cout<< position; //计算数组长度 cout<<" the array length :"<<length<<endl; } /* //出现此类错误:error C2664: 'a' : cannot convert parameter 1 from 'int [2]' to 'int &' A reference that is not to 'const' cannot be bound to a non-lvalue //引用传址: 传递了别引用对象的地址。一次只能传递一个地址 //int a(int &array[2]) //int a(int &array) { return 1; } void main() { int array[2]={1,2}; int b =a(array); cout<<a; } */