C++ search search_n

萧奇
2023-12-01
#include<vector>
#include<iostream>
#include<list>
#include<algorithm>
using namespace std;

int main(){
	vector <int> a;
	for(int i = 0; i < 10; i ++)
		a.push_back( i);
	a.push_back( 9);
	a.push_back( 9);
	
	for(auto i = a.begin(); i != a.end(); i ++)
		cout << *i << ' ';
	cout << endl;
	
	list <int> b;
	for( int i = 4; i <= 8; i ++)
		b.push_back( i);
	
	for(auto i = b.begin(); i != b.end(); i ++)
		cout << *i << ' ';
	cout << endl;
	
	// search( iterator first1, iterator last1, iterator first2, iterator last2)
	auto p = search( a.begin(), a.end(), b.begin(), b.end());
	if( p != a.end()) cout << "sequence b found in position " << distance( a.begin(), p) << endl;
	else cout << "sequence not found" << endl;
	
	cout <<endl;
	
	//search_n ( iterator first, iterator last, n, key)
	auto i = search_n( a.begin(), a.end(), 3, 9);
	if( i != a.end()) cout << "999 found in position " << distance( a.begin(), i);
	else cout << "999 not found" << endl;
}
 类似资料: