Реализуйте search() (параграф 18.5.5). Обеспечьте оптимизированную версию для итераторов произвольного доступа.
Ну тут примерно как то так нужно сделать:
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include <iostream> using std::cout; using std::endl; #include <algorithm> using std::search; #include <cstdlib> using std::exit; #include <vector> using std::vector; template<class T> T search1(T n,T e,T n1,T e1) { while(n!=e) { if(*n==*n1) { T prom=n; T prom1=n1; //cout <<"mu tyt"<<endl;exit(1); while(n1!=e1) { //проверка равно ли if(*n!=*n1) break; //cout <<"mu tyt"<<endl;exit(1); //проверка на конец //n1++;n++; //cout <<"*n1= "<<*n1<<" *n= "<<*n<<endl; if(n1==--e1&&*n1==*n) { return prom; } else if(n1==--e1&&*n1!=*n) { break; } else { //cout <<"mu tyt"<<endl;exit(1); e1++; } n1++; n++; }//конец while n=prom; n1=prom1; }//конец if (начало проверки) n++; }//конец while return e; } int main() { int mass[]={1,2,3,4,5,6}; vector<int> v(mass,mass+6); int mass1[]={3,4,5}; vector<int> v1(mass1,mass1+3); vector<int>::iterator it1; int* it; it=search1(mass,mass+6,mass1,mass1+3); if(it!=mass+sizeof(mass)/sizeof(int)) cout <<"*it= "<<(it-mass)<<endl; else cout <<"ne naideno"<<endl; it1=search1(v.begin(),v.end(),v1.begin(),v1.end()); if(it1!=v.end()) cout <<"*it= "<<(it1-v.begin())<<endl; else cout <<"ne naideno"<<endl; return 0; } |
[youtube]http://www.youtube.com/watch?v=bnRlxs9BQTM[/youtube]