STL算法------查找4

find_first_of(b, e, sb, se )         找sb,se中间的任意一个

find_first_on(b ,  e, sb, se, bp )


使用逆向迭代器            没有find_last_of算法


string和stl查找的区别

               string函数                             stl算法

               find()                                        find()

               rfind()                                       find()+逆向迭代器

               find()                                        search()

               rfind()                                       find_end()

               find_first_of()                          find_first_of() 

               find_last_of()                          find_first_of()   + 逆向迭代器


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
using namespace std;

int main( int argc, char** argv )
{
	vector<int> vec;
	list<int> lst;

	for( int i=1; i<12; ++i )
	{
		vec.insert(vec.end(), i );
	}
	lst.push_back(3);
	lst.push_back(6);
	lst.insert(lst.end(), 9 );
	for(vector<int>::iterator itr = vec.begin(); itr!=vec.end(); ++itr )
	{
		cout<<*itr<<' ';
	}
	cout<<endl;

	vector<int>::iterator pos;
	pos = find_first_of( vec.begin(), vec.end(), lst.begin(), lst.end());
	if( pos != vec.end() )
	{
		cout<<"find pos: "<<distance(vec.begin(), pos)+1<<endl;
	}else{
		cout<<"not find"<<endl;
	}

	vector<int>::reverse_iterator rpos;
	rpos = find_first_of( vec.rbegin(), vec.rend(), lst.begin(), lst.end() );
	if( rpos != vec.rend() )
	{
		cout<<"find rpos: "<<distance(vec.begin(), rpos.base())<<endl;
	}else{
		cout<<"not find"<<endl;
	}

	//string
	string strNum("0123456789");
	string name("r2d3k");
	string::size_type p =name.find_first_of(strNum);
	if( string::npos != p )
	{
		cout<<"find pos:"<<p<<endl;
	}else{
		cout<<"not find"<<endl;
	}
	p =name.find_last_of(strNum);
	if( string::npos != p )
	{
		cout<<"find pos:"<<p<<endl;
	}else{
		cout<<"not find"<<endl;
	}

	return 0;

}



       



posted @ 2015-03-04 14:27  SandKing  阅读(7)  评论(0)    收藏  举报  来源