STL算法------查找1

1.find()

2.find_if()

3.search_n()

4.search()

5.find_end()

6.find_first_of()

7.adjacent_find()


已序区间查找算法:

   binary_search()      includes()     lower_bound()     upper_bound()


注意:

       1.如果是已序区间,可以使用已序区间查找算法

        2.关联式容器有等效的成员函数find()

        3.string有等效的成员函数find()


#include <iostream>
#include <algorithm>
#include <list>

using namespace std;

int main( int argc, char** argv )
{
	list<int> ilst;
	for( int i=0; i<9; ++i )
	{
		ilst.insert(ilst.end(), i);
	}
	for( int i=0; i<9; ++i )
	{
		ilst.insert(ilst.end(), i);
	}

	for( list<int>::const_iterator citr = ilst.begin(); citr!=ilst.end(); ++citr )
	{
		cout<<*citr<<' ';
	}
	cout<<endl;

	list<int>::iterator pos1;
	pos1 = find(ilst.begin(), ilst.end(), 4);
	list<int>::iterator pos2;
	if( pos1 != ilst.end() )
	{
		pos2 = find( ++pos1, ilst.end(), 4 );
	}
	for(list<int>::iterator itr = pos1; itr!= pos2; ++itr)
	{
		cout<<*itr<<' ';
	}
	cout<<endl;

	return 0;
}

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <functional>

using namespace std;

int main( int argc, char** argv )
{
	vector<int> vec;
	vector<int>::iterator pos;

	for( int i=1; i<9; ++i )
	{
		vec.insert( vec.end(), i );
	}

	for(vector<int>::const_iterator citr = vec.begin(); citr!=vec.end(); ++citr)
	{
		cout<<*citr<<' ';
	}
	cout<<endl;

	pos = find_if(vec.begin(), vec.end(), bind2nd(greater<int>(), 3));
	cout<<*pos<<endl;

	pos = find_if(vec.begin(), vec.end(), not1(bind2nd(modulus<int>(), 3)));
	cout<<*pos<<endl;

	return 0;
}

#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
#include <functional>
#include <map>
#include <string>
//#include <stdlib.h>
//#include <stdio.h>

using namespace std;

int main( int argc, char** argv )
{
	map<int, string> m;
	string str;
	char Buf[255];

	for( int i=0; i<9; ++i )
	{
		sprintf( Buf, "%s%d", "zhang", i );
		m.insert( make_pair( i, str.assign(Buf) ) );
	}

	for(map<int, string>::const_iterator citr=m.begin(); citr!=m.end(); ++citr)
	{
		cout<<citr->first<<' ';
		cout<<citr->second<<endl;
	}
	//map----find
	map<int,string>::iterator pos;
	pos = m.find(3);
	if( pos != m.end() )
	{
		cout<<pos->first<<' '<<pos->second<<"   find"<<endl;
	}

	//string---find
	string strFind("annaBell");
	string::size_type pos1 = strFind.find("nna");
	if( pos1 != string::npos )
	{
		cout<<pos1<<"  "<<strFind.at(pos1)<<endl;
	}

	return 0;
}


posted @ 2015-03-03 18:57  SandKing  阅读(1)  评论(0)    收藏  举报  来源