类属性算法find分别用于数组、表和输入迭代器
1
#include
<
iostream
>
2 #include < cassert >
3 #include < algorithm >
4 #include < list >
5 #include < iterator >
6
7 using namespace std;
8
9 int main()
10 {
11 int a[ 10 ] = { 12 , 3 , 25 , 7 , 11 , 213 , 7 , 123 , 29 , - 31 };
12 // int *ptr=find(&a[0],&a[10],7);
13 // assert (*ptr==7 && *(ptr+1)==11);
14 list < int > list1( & a[ 0 ], & a[ 10 ]);
15 list < int > ::iterator i = find(list1.begin(),list1.end(), 7 );
16 assert ( * i == 7 && * ( ++ i) == 11 );
17
18 cout << " Type some characters,including an 'x' followed\n " << " by at least one nonwhite-space character: " << flush;
19 istream_iterator < char > in (cin);
20 istream_iterator < char > eos;
21 find( in ,eos, ' x ' );
22 cout << " The first nonwhite-space character following\n " << " the first 'x' was ' " <<* ( ++ in ) << " '. " << endl;
23
24 return 0 ;
25 }
2 #include < cassert >
3 #include < algorithm >
4 #include < list >
5 #include < iterator >
6
7 using namespace std;
8
9 int main()
10 {
11 int a[ 10 ] = { 12 , 3 , 25 , 7 , 11 , 213 , 7 , 123 , 29 , - 31 };
12 // int *ptr=find(&a[0],&a[10],7);
13 // assert (*ptr==7 && *(ptr+1)==11);
14 list < int > list1( & a[ 0 ], & a[ 10 ]);
15 list < int > ::iterator i = find(list1.begin(),list1.end(), 7 );
16 assert ( * i == 7 && * ( ++ i) == 11 );
17
18 cout << " Type some characters,including an 'x' followed\n " << " by at least one nonwhite-space character: " << flush;
19 istream_iterator < char > in (cin);
20 istream_iterator < char > eos;
21 find( in ,eos, ' x ' );
22 cout << " The first nonwhite-space character following\n " << " the first 'x' was ' " <<* ( ++ in ) << " '. " << endl;
23
24 return 0 ;
25 }