简单的程序诠释C++ STL算法系列之五:find_first_of

        C++STL的非变易算法(Non-mutating algorithms)是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。

        find_first_of算法用于查找位于某个范围之内的元素。它有两个使用原型,均在迭代器区间[first1, last1)上查找元素*i,使得迭代器区间[first2, last2)有某个元素*j,满足*i ==*j或满足二元谓词函数comp(*i, *j)==true的条件。元素找到则返回迭代器i,否则返回last1。

函数原型:

template<class ForwardIterator1, class ForwardIterator2>
   ForwardIterator1 find_first_of(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2
   );
template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
   ForwardIterator1 find_first_of(
      ForwardIterator1 _First1, 
      ForwardIterator1 _Last1,
      ForwardIterator2 _First2, 
      ForwardIterator2 _Last2,
      BinaryPredicate _Comp
   );
 

示例代码:

#include <algorithm>
#include <iostream>

using namespace std;

int main()
{
	const char* strOne = "abcdef1212daxs";
	const char* strTwo = "2ef";

	const char* result = find_first_of(strOne, strOne + strlen(strOne),
								 strTwo, strTwo + strlen(strTwo));

	cout << "字符串strOne中第一个出现在strTwo的字符为:"
		 << *result << endl;

	return 0;
}

*******************************************************************************************************************************

C++经典书目索引及资源下载:http://blog.csdn.net/jerryjbiao/article/details/7358796

********************************************************************************************************************************

 

点赞