您的位置:首页 > 其它

find()、find_end()、find_first_of()、find_if()的使用示例和区别

2009-05-28 02:08 525 查看
http://blog.csdn.net/xqls_xqls/archive/2008/12/03/3437640.aspx

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#include <string>
using namespace std;
int main(int argc,_TCHAR* argv[])
{
//find_first_of()的使用
int nums1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* result1;
int start = 0;
int end = 10;
int targets[] = { 9, 11, 7 };
result1 = find_first_of( nums1 + start, nums1 + end, targets + 0, targets + 2 );
if( *result1 == nums1[end] )
cout << "Did not find any of { 9, 11, 7 }" << endl;
else
cout << "Found a matching target: " << *result1 << endl;

//find_end()的使用
int nums[] = { 11, 2, 3, 4, 1, 2, 3, 4, 11, 2, 3, 4 };
int* result;
end = 11;
int target1[] = { 1, 2, 3 };
result = find_end( nums + start, nums + end, target1 + 0, target1 + 2 );
if( *result == nums[end] )
cout << "Did not find any subsequence matching { 1, 2, 3 }" << endl;
else
cout << "The last matching subsequence is at: " << *result << endl;

int target2[] = { 3, 2, 3 };
result = find_end( nums + start, nums + end, target2 + 0, target2 + 2 );
if( *result == nums[end] )
cout << "Did not find any subsequence matching { 3, 2, 3 }" << endl;
else
cout << "The last matching subsequence is at: " << *result << endl;

//find()的使用
int num_to_find = 3;
vector<int> v1;
for( int i = 0; i < 10; i++ )
v1.push_back(i);

vector<int>::iterator result2;
result2 = find( v1.begin(), v1.end(), num_to_find );
if( result2 == v1.end() )
cout << "Did not find any element matching " << num_to_find << endl;
else
cout << "Found a matching element: " << *result2 << endl;

//find_if()的使用
int nums3[] = { 0, -1, -2, -3, -4, 342, -5 };
start = 0;
end = 7;
result = find_if( nums3 + start, nums3 + end, bind2nd(greater<int>(), 0));
if( *result == nums3[end] )
cout << "Did not find any number greater than zero" << endl;
else
cout << "Found a positive non-zero number: " << *result << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐