// search.cpp -- 2011-10-01-23.31
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using std ::vector ;
using std ::not_equal_to ;
int _tmain(int argc, _TCHAR* argv[])
{
int arr1[] = {1, 2, 3, 4, 5, 7, 8, 9} ;
vector<int> vec1(arr1, arr1 + sizeof arr1 / sizeof (int)) ;
int arr2[] = {1, 0, 3, 4} ;
vector<int> vec2(arr2, arr2 + sizeof arr2 / sizeof (int)) ;
// search (beg1, end1, beg2, end2) ;
// 操作前:[beg1,end1)标示第一个输入序列.[beg2,end2)标示第二个输入序列.
// 操作后:确定[beg1,end1)输入序列中是否存在[beg2,end2)序列.
// 返回值:如果[beg1,end1)中存在[beg2,end2)序列,返回指向该序列在[beg1,end1)中第一次出现位置的迭代器.
// 否则返回end1.
// 备注: beg1和beg2的类型必须兼容,否则将无法通过编译.
vector<int> ::iterator iter = search (vec1.begin(), vec1.end(), vec2.begin(), vec2.end()) ;
if (iter != vec1.end())
std ::cout << *iter << std ::endl ;
// search (beg1, end1, beg2, end2, equal_to<int> ()) ;
// 操作前:[beg1,end1)标示第一个输入序列.[beg2,end2)标示第二个输入序列.equal_to<int> ()操作符是二元函数对象.
// 操作后:确定[beg1,end1)输入序列中是否存在[beg2,end2)序列.
// 返回值:如果[beg1,end1)中存在[beg2,end2)序列,返回指向该序列在[beg1,end1)中第一次出现位置的迭代器.
// 否则返回end1.
// 备注: beg1和beg2的类型必须兼容,否则将无法通过编译.
// 相等元素是使equal<int> ()为真的元素.
iter = search (vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), not_equal_to<int> ()) ;
if (iter != vec1.end())
std ::cout << *iter << std ::endl ;
std ::cin.get() ;
return 0 ;
}
search
最新推荐文章于 2024-08-23 12:37:49 发布