C++ STL的非变易算法是一组不破坏操作数据的模板函数,用来对序列数据进行逐个处理、元素查找、子序列搜索、统计和匹配。非变易算法具有极为广泛的适用性,基本上可应用于各种容器。
C++ STL提供了一个for_each函数,用于对容器的元素进行循环操作。它对迭代区间[first, last)所指的每一个元素,执行由单参函数对象fn所定义的操作。原型如下:
template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function fn)
{
while (first!=last) //循环遍历
{
fn (*first);//调用fn进行操作
++first;
}
return fn;
}
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
struct print{
int count; //打印的元素计数
print(){count=0;}
void operator()(int x){
cout << 3*x << endl;
count++;
}
};
int main(void){
//双向链表初始化
list<int> l;
l.push_back(29);
l.push_back(32);
l.push_back(16);
l.push_back(22);
l.push_back(27);
//打印链表的元素
print p=for_each(l.begin(),l.end(),print());
//打印的元素个数
cout << p.count << endl;
return 0;
}
find算法函数用来查找等于某值的元素。它在迭代器区间[first,last)上查找等于value值的元素,如果迭代器i所指的元素满足*i==value,则返回迭代器i。未找到满足条件的元素,返回last。
//find算法函数的代码
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
#include <algorithm>
#include <list>
#include <iostream>
int main(void){
using namespace std;
//双向链表初始化
list<int> l;
l.push_back(10);
l.push_back(18);
l.push_back(26);
l.push_back(26);
l.push_back(30);
//查找元素26
list<int>::iterator iLocation=find(l.begin(),l.end(),26);
if(iLocation != l.end())
cout << "找到元素26" << endl;
//打印元素18
cout << "前一个元素为" << *(--iLocation) << endl;
return 0;
}
find_if算法函数是find的一个判断版本,它利用bool值谓词判断pred,检查迭代区间[first,last)上的每一个元素,如果迭代器i满足pred(*i)==true,表示找到元素并返回迭代器i;未找到元素,返回末位置last。
template<class InputIterator, class UnaryPredicate>
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last) {
if (pred(*first)) return first;
++first;
}
return last;
}
#include <algorithm>
#include <vector>
#include <iostream>
bool divby5(int x){
return x % 5 ? 0 : 1;
}
int main(void){
using namespace std;
//初始化vector
vector<int> v(20);
for(unsigned int i=0; i<v.size(); i++)
v[i]=(i+1)*(i+3);
vector<int>::iterator iLocation;
iLocation=find_if(v.begin(), v.end(), divby5);
if(iLocation != v.end())
cout << "找到第一个能被5整除的元素"
<< *iLocation << endl
<< "元素的索引位置为"
<< iLocation - v.begin() << endl;
system("pause");
return 0;
}
adjacent_find算法函数用于查找相等或满足条件的邻近元素对。它有两个使用原型如下:
template <class ForwardIterator>
ForwardIterator adjacent_find (ForwardIterator first, ForwardIterator last)
{
if (first != last)
{
ForwardIterator next=first; ++next;
while (next != last) {
if (*first == *next)
return first;
++first; ++next;
}
}
return last;
}
template<class ForwardIter, class BinaryPredicate>
ForwardIter adjacent_find(ForwardIter first, ForwardIter last,BinaryPredicate binary_pred)
{
if (first==last)
return last;
ForwardIter next=first;
while(++next!=last)
{
if (binary_pred(*first,*next))//两个邻近元素是否满足binary_pred条件
return first;
first=next;
}
return last;
}
#include <algorithm>
#include <list>
#include <iostream>
bool parity_equal(int x, int y){
return (x-y)%2 == 0 ? 1:0;
}
int main(void){
using namespace std;
//链表初始化
list<int> l;
l.push_back(3);
l.push_back(6);
l.push_back(9);
l.push_back(11);
l.push_back(11);
l.push_back(18);
l.push_back(20);
l.push_back(20);
//查找邻接相等的元素
list<int>::iterator iResult=adjacent_find(l.begin(),l.end());
if(iResult != l.end()){
cout << "发现链表有两个邻接的元素相等:" << endl;
cout << *iResult << endl;
iResult++;
cout << *iResult << endl;
}
//查找奇偶性相同的邻接元素
iResult=adjacent_find(l.begin(), l.end(), parity_equal);
if(iResult != l.end()){
cout << "发现有两个邻接元素的奇偶性相同: " << endl;
cout << *iResult << endl;
iResult++;
cout << *iResult << endl;
}
system("pause");
return 0;
}
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,
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 find_first_of (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,BinaryPredicate pred);
//find_first_of算法函数的代码
template<class InputIterator, class ForwardIterator>
InputIterator find_first_of ( InputIterator first1, InputIterator last1,
ForwardIterator first2, ForwardIterator last2)
{
while (first1!=last1) {
for (ForwardIterator it=first2; it!=last2; ++it) {
if (*it==*first1) // or: if (pred(*it,*first)) for version (2)
return first1;
}
++first1;
}
return last1;
}
#include <algorithm>
#include <iostream>
int main(void)
{
using namespace std;
//定义两个字符串
char* string1="abcdef7ghijklmn";
char* string2="zyx3pr7ys";
//范围查找string1于string2中
char* result=find_first_of(string1, string1 + strlen(string1),
string2, string2 + strlen(string2));
cout << "字符串string1的第一个出现在string2的字符为:"
<< *result << endl;
return 0;
}
未完待续……
转载请注明出处:http://blog.csdn.net/lsh_2013/article/details/46846027
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/lsh_2013/article/details/46846027