非修改性序列算法

该算法不改动容器中元素的次序,也不改动元素值。一般通过input迭代器和forward迭代器完成工作,可用于所有的标准容器。

一.for_each算法:for_each(iterator begin,iterator end,proc op)

该算法对区间[begin,end)中的每个元素调用进程op

①不修改元素

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int a)
{
	cout << a << endl;
}
int main()
{
	vector<int>vi;
	for (int i = 1; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	for_each(vi.begin(), vi.end(), 
		[](int b) //lambda
		{
			cout <<b << endl;
		}
	);
	for_each(vi.begin(),vi.end(),print);//或者
	return 0;
}
/*
1
3
5
7
9
*/

②修改元素

#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int a)
{
	cout << a << endl;
}
int main()
{
	vector<int>vi;
	for (int i = 1; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	for_each(vi.begin(), vi.end(), 
		[](int &b) //引用
		{
			b+=100;
		}
	);
	for_each(vi.begin(),vi.end(),print);
	return 0;
}
/*
101
103
105
107
109
*/

③使用其返回值:例如返回sum求和

二.元素计数

count(Iterator start,Iterator end,value)

count(Iterator start,Iterator end,Condition)

#include <iostream>
#include<vector>
#include<functional>//bind2nd()
#include<algorithm>
using namespace std;

bool isEven(int elem)
{
	return elem % 2 == 0;
}
int main()
{
	vector<int>vi;
	for (int i = 0; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	cout << count_if(vi.begin(),vi.end(),isEven)<<endl;//统计偶数的个数:5
	cout << count_if(vi.begin(), vi.end(), bind2nd(greater<int>(), 2));//统计比2大的数的个数:4
	return 0;
}

三.最大值最小值

最小值:

Iterator min_element(Iterator beg,Iterator end);

Iterator min_element(Iterator beg,Iterator end,comFunc op);

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

bool absless(int elem1,int elem2)
{
	return abs(elem1) < abs(elem2);
}

int main()
{
	vector<int>vi;
	for (int i = -8; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	cout << *min_element(vi.begin(), vi.end()) << endl;//输出向量中最小的元素:-8
	cout << *min_element(vi.begin(), vi.end(),absless) << endl;//输出向量绝对值中最小的元素:0
	return 0;
}

四.搜寻元素

1.搜寻第一个匹配元素

Iterator find(Iterator beg,Iterator end,value);

Iterator find_if(Iterator beg,Iterator end,value,ConditionFunc);

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;


int main()
{
	vector<int>vi;
	for (int i = -8; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	cout << *find(vi.begin(),vi.end(),0) << endl;//查找0,失败则返回end迭代器
	cout << *find_if(vi.begin(), vi.end(),bind2nd(greater<int>(),2)) << endl;//输出向量第一个大于2的元素:4
	return 0;
}

2.搜寻前n个连续匹配值

Iterator search_n(Iterator beg,Iterator end,Size count,value);返回连续count等于value的元素的起始位置

Iterator search_n(Iterator beg,Iterator end,Size count,value,BinaryPredicate op);返回连续count满足op的元素的起始位置

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;


int main()
{
	vector<int>vi;
	vector<int>::iterator it;
	for (int i = -2; i < 10; i += 2)
	{
		vi.push_back(i);
	}
	vi.push_back(5);
	vi.push_back(5);
	vi.push_back(5);
	it = search_n(vi.begin(), vi.end(), 3, 5);
	cout << distance(vi.begin(),it) << endl;//返回3个连续等于5的元素的起始位置:6
	it = search_n(vi.begin(), vi.end(), 3, 4, greater<int>());
	cout << distance(vi.begin(), it) << endl;//返回3个连续大于4的元素的起始位置:4
	return 0;
}

3.搜寻第一个子区间

Iterator search(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end);iterator1原串,iterator子串

Iterator search(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end,BinaryPredicate op);

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;

bool cmp(int elem,bool even)
{
	if (even)
	{
		return elem % 2 == 0;
	}
	else
		return elem % 2 == 1;
}

int main()
{
	vector<int>v1,v2;
	vector<int>::iterator it;
	bool check[] = { true,false,true };
	for (int i = -2; i < 10; i += 2)
	{
		v1.push_back(i);
	}
	for (int i = 4; i < 10; i += 2)
	{
		v2.push_back(i);
	}
	it = search(v1.begin(),v1.end(),v2.begin(),v2.end());
	cout << distance(v1.begin(),it) << endl;//返回子串在原串的起始位置:3
	it = search(v1.begin(),v1.end(),check,check+3,cmp);
	cout << distance(v1.begin(),it) << endl;//返回满足偶奇偶顺序的子串起始位置:6(即没有)
	return 0;
}

4.搜寻最后一个子区间(类似于3)

Iterator find_end(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end);iterator1原串,iterator子串

Iterator find_end(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end,BinaryPredicate op);

5.搜寻某些元素的第一次出现位置(类似于search)

Iterator find_first_of(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end);iterator1原串,iterator子串

Iterator find_first_of(Iterator1 beg,Iterator1 end,Iterator2 beg,Iterator2 end,BinaryPredicate op);

6.搜寻两个连续相等的元素(或者连续满足条件的)

Iterator adjacent_find(Iterator beg,Iterator end);

Iterator adjacent_find(Iterator beg,Iterator end,Predicate op);

#include <iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;

bool doubles(int elem1,int elem2)
{
	return elem1 * 2 == elem2;
}

int main()
{
	vector<int>v1;
	vector<int>::iterator it;
	v1.push_back(-2);
	for (int i = -2; i < 10; i += 2)
	{
		v1.push_back(i);
	}
	it = adjacent_find(v1.begin(),v1.end());
	cout << distance(v1.begin(),it) << endl;//返回连续相等元素的起始位置:3
	it = adjacent_find(v1.begin(),v1.end(),doubles);
	cout << distance(v1.begin(),it) << endl;//返回第二个元素是第一个元素两倍起始位置:3
	return 0;
}
0

发表评论

邮箱地址不会被公开。 必填项已用*标注