STL常用算法(循环,查询,计数,比较)

//循环,查询,计数,比较

序号 功能 函数名称 说明  
1 循环 for_each 遍历容器元素,对每个元素执行相同的操作  
2 查询 find 在单迭代器序列中找出某个值第一次出现的位置  
find_if 在单迭代器序列中找出符合某谓词的第一个元素  
find_first_of 在双迭代器序列中找出子序列中某元素第一次出现的位置  
adjacent_find 在单迭代器序列中找出第一次相邻值相等元素出现的位置  
find_end 在双迭代器序列中找出子序列中最后一次出现的位置  
search 在双迭代器序列中找出子序列中第一次出现的位置  
search_n 在单迭代器序列中找出某个值连续出现n次出现的位置  
3 计数 count 在序列中统计某个值出现的次数  
count_if 在序列中统计某个值与某谓词(表达式)匹配的次数  
4 比较 equal 在两个序列中对应元素都相同时为真  
mismatch 找出两个序列相异的第一个元素  
         

#include"iostream"
#include"vector"
#include"algorithm"
#include"functional"
#include"string"
#include"iterator"
using namespace std;
/*for_each和transform*/
/* for_each: 用指定函数依次对指定范围内所有元素进行迭代访问。 该函数不得修改
序列中的元素。
 函数定义。 For_each(begin, end, func);
*/
void print(int &a)
{
	cout << a << " ";
}
template <class T>
class paixu
{
public:
	paixu()
	{
		num = 0;
	}
public:
	
	bool operator()(T &a)
	{
		num++;
		cout << a << " ";
		return (a < 10);
	}
	void printnum()
	{
		cout << "num: " << num<<endl;
	}
private:
	int num;
};
/* transform: 与 for_each 类似, 遍历所有元素, 但可对容器的元素进行修改
 transform()算法有两种形式:
 transform(b1, e1, b2, op)
 transform(b1, e1, b2, b3, op)
可以一个容器的元素, 通过 op, 变换到另一个容器中(同一个容器中)
也可以把两个容器的元素, 通过 op, 变换到另一个容器中
*/
int increase(int n)
{
	return  n + 100;
}
void printf(vector<int>& v2)
{
	for (vector<int>::iterator it = v2.begin(); it != v2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main01()
{//for_each
/*template<class _InIt,
	class _Fn1> inline
	_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
	{	// perform function for each element
	_DEBUG_RANGE(_First, _Last);
	_DEBUG_POINTER(_Func);
	_For_each(_Unchecked(_First), _Unchecked(_Last), _Func);

	return (_STD move(_Func));
	}
	*/
	cout << "for_each: " << endl;
	vector<int>v1;
	for (int i = 0; i < 20; i++)
	{
		v1.push_back(rand()%100);
	}
	//函数对象、回调函数入口地址(函数必须有且只有一个参数)
	for_each(v1.begin(),v1.end(),print);//41 67 34 0 69 24 78 58 62 64 5 45 81 27 61 91 95 42 27 36
	cout << endl;
	paixu<int> p=for_each(v1.begin(), v1.end(), paixu<int>());
	p.printnum();
	cout << endl;

//transform
	cout << "transform: " << endl;
	vector<int>v2 = v1;
	//回调函数
	cout << "回调函数+100: " << endl;
	transform(v2.begin(), v2.end(), v2.begin(), increase);
	printf(v2);
	//预定义函数对象
	cout << "预定义函数对象取反: " << endl;
	transform(v2.begin(), v2.end(), v2.begin(), negate<int>());
	printf(v2);
	//函数适配器
	cout << "函数适配器*10: " << endl;
	transform(v2.begin(),v2.end(),v2.begin(),bind2nd(multiplies<int>(),10));
	printf(v2);
	//把结果输出到屏幕
	cout << "把结果输出到屏幕/10取反: " << endl;
	transform(v2.begin(), v2.end(), v2.begin(), negate<int>());
	printf(v2);
	transform(v2.begin(), v2.end(),ostream_iterator<int>(cout," "), bind2nd(divides<int>(), 10));
	
	return 0;
//for_each和transform的区别(请看源码)
//一般情况下: for_each 所使用的函数对象, 参数是引用, 没有返回值
// transform 所使用的函数对象, 参数一般不使用引用, 而是还有返回值
}
//////////////////查询算法//////////////////////////////////////////////////////////////////////////////////////
/*函数有:find、find_if、find_first_of、adjacent_find、find_end、search、search_n、binary_search*/
void a_print(int &n)
{
	cout << n << " ";
}
bool a_print_3(int &n)
{
	if (n > 3)
		return true;
	else
		return false;
}
void main02()
{
	int a[] = {1,2,3,3,3,4,5,6,7,8,8,9 };
	int b[] = {3,4,5};
	vector<int>v1(a, a + sizeof(a) / sizeof(int));
	vector<int>v2(b, b + sizeof(b) / sizeof(int));
	for_each(v1.begin(),v1.end(),a_print);
	cout << endl;
	vector<int>::iterator  find1 = find(v1.begin(), v1.end(), 5);
	cout << "输出=5第一次出现的位置和值:" << distance(v1.begin(), find1)<<","<<*find1 << endl;//6,5
	vector<int>::iterator  findif1 = find_if(v1.begin(),v1.end(),a_print_3);
	cout << "输出>3第一次出现的位置和值:" << distance(v1.begin(), findif1)<<" ," << *findif1 << endl;//5,4
	vector<int>::iterator find_first_of1 = find_first_of(v1.begin(),v1.end(),v2.begin(),v2.end());
	cout << "输出v2某元素第一次出现的位置和值:" << distance(v1.begin(), find_first_of1) << " ," << *find_first_of1 << endl;
	vector<int>::iterator adjacent1 = adjacent_find(v1.begin(),v1.end());//2,3(返回第一个3的位置)
	cout << "第一次出现相邻值相等的元素的位置和值:" << distance(v1.begin(), adjacent1) << " ," << *adjacent1 << endl;//2,3
	vector<int>::iterator findend = find_end(v1.begin(), v1.end(), v2.begin(), v2.end());
	cout << "输出v2最后一次完全匹配出现的位置和值:" << distance(v1.begin(), findend) << " ," << *findend << endl;//4,3(返回最后一个3的位置)
	vector<int>::iterator search1 = search(v1.begin(), v1.end(), v2.begin(), v2.end());
	cout << "输出v2首次完全匹配出现的位置和值:" << distance(v1.begin(), search1) << " ," << *search1 << endl;//4,3
	vector<int>::iterator search_n1 = search_n(v1.begin(), v1.end(), 2 ,8);
	cout << "输出v1中2个8出现的位置和值:" << distance(v1.begin(), search_n1) << " ," << *search_n1 << endl;//9,8
	//二分法查找某值,返回值为bool型,且只针对已从小到大排序完成的数组类型
	bool A = binary_search(v1.begin(),v1.end(),5);
	if (A = true)
	{
		cout << "已找到" << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}
/////////////////////////计数/////////////////////////////////////////////////////////////////////
//计数count、count_if
void main03()
{
	int a[] = { 1, 2, 3, 3, 3, 4, 5, 6, 7, 8, 8, 9 };
	vector<int>v1(a,a+sizeof(a)/sizeof(int));
	for_each(v1.begin(), v1.end(), a_print);
	cout << endl;
	int num1 = count(v1.begin(),v1.end(),8);//8的个数
	cout << "=8的个数num1= " << num1 << endl;//2
	int num2 = count_if(v1.begin(), v1.end(), a_print_3);///>3的个数
	cout << ">3的个数num2= " << num2 << endl;//7
}
////////////////////////////比较///////////////////////////////////////////////////////////////////
//比较equal、mismatch
void main04()
{
	int a[] = {1, 2, 3, 3, 3, 4, 5, 6, 7, 8, 8, 9 };
	int b[] = {1,2, 3, 4, 5, 6, 7, 8, 8 };
	vector<int>v1(a, a + sizeof(a) / sizeof(int));
	vector<int>v2(b, b + sizeof(b) / sizeof(int));
	bool A=equal(v1.begin(),v1.end(),v2.begin());//对应元素相等时为真;
	cout << A << endl;  //0
	pair<vector<int>::iterator, vector<int>::iterator>p = mismatch(v1.begin(), v1.end(), v2.begin());//找出两序列第一对元素不相等的迭代器指针
	cout << "v1位置:" << distance(v1.begin(), p.first) << " " << "v1值:" << *p.first << "\n" << "v2位置:" << distance(v2.begin(), p.second) << " " << "v2值:" << *p.second << endl;//v1位置: 3  v1值:  3  //v2位置: 3  v2值 : 4

}
int main()
{

	//main01();//for_each和transform
	//main02();//查询find、find_if、find_first_of、adjacent_find、find_end、search、search_n、binary_search
	//main03();//计数count、count_if
	main04();//比较equal、mismatch
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ukston_c/article/details/80700828