1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*template <typename aType>
inline aType* begin(const vector<aType> &vec) {
return vec.empty()? 0: &vec[0];
}*/
template <typename aType>
aType* find1(const aType* first, const aType* last, const aType &value) {
if(!first||!last) return 0;
for( ;first!=last;++first) {
if(*first==value) return first;
}
return 0;
}
int main() {
int ia[8]={1,1,2,3,5,8,13,21};
double da[6]={1.5,2.0,2.5,3.0,3.5,4.0};
string sa[4]={"pooh", "piglet", "eeyore", "tigger"};
cout << "pi=" << &ia<<endl;
cout << "pd=" << &da<<endl;
cout << "ps=" << &sa<<endl;
int *pi=find1(ia,ia+8,ia[3]);
double *pd=find1(da,da+6,da[3]);
string *ps=find1(sa,sa+4,sa[3]);
cout << "pi=" << &ia<<endl;
cout << "pd=" << &da<<endl;
cout << "ps=" << &sa<<endl;
cout << "pi=" << pi<<endl;
cout << "pd=" << pd<<endl;
cout << "ps=" << ps<<endl;
return 0;
}
|