STL学习之十四:常用算法 for_each 和 transform以及adjacent_find的示例代码

本文主要介绍STL中常用算法 for_each 和 transform以及adjacent_find示例代码,具体用法示例如下:

// 1  for_each :用指定函数依次对指定范围内所有元素进行迭代访问 该函数不得修改序列中的元素
//template inline
//	_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
//{	// perform function for each element
//	_DEBUG_RANGE(_First, _Last);
//	_DEBUG_POINTER(_Func);
//	return (_For_each(_Unchecked(_First), _Unchecked(_Last), _Func));
//}
// 返回的是第三个参数的 函数对象类型


// 2 transform :与for_each类似,遍历所有元素,但可以对容器的元素进行修改

#include "iostream"
using namespace std;

#include "vector"
#include "set"
#include "queue"
#include "list"
#include "algorithm"
#include "string"
#include "functional"
#include "iterator"
//template 
//void printMy(T &my)
//{
//	for (T::iterator it=my.begin();it!=my.end();it++)
//	{
//		cout << *it << " ";
//	}
//	cout << endl;
//}
void printV(vector &v)
{
	for (vector::iterator it=v.begin();it!=v.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void printList(list &v)
{
	for (list::iterator it=v.begin();it!=v.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void showElem(int &n)
{
	cout << n << " ";
}

class CMyElem
{
public:
	CMyElem()
	{
		num = 0;
	}
	void operator()(int &n)
	{
		num++;
		cout << n << " ";
	}
	void printNum()
	{
		cout << "num:" << num << endl;
	}
protected:

private:
	int num;
};
void main31_foreach()
{
	vector v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	printV(v1);//printMy>(&v1);//void printMy(T &my)

	for_each(v1.begin(),v1.end(),showElem);
	cout << endl;

	for_each(v1.begin(),v1.end(),CMyElem());
	cout << endl;

	CMyElem my1 = for_each(v1.begin(),v1.end(),CMyElem());// 返回的是第三个参数的 函数对象类型
	my1.printNum();

}

int increase(int i)
{
	return i+100;
}
void main32_transform()
{
	vector v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	//printMy>(&v1);//void printMy(T &my)
	// transform 使用回调函数
	transform(v1.begin(),v1.end(),v1.begin(),increase);
	printV(v1);//printMy>(&v1);//void printMy(T &my)

	// transform 使用预定义的函数对象
	transform(v1.begin(),v1.end(),v1.begin(),negate());
	printV(v1);//printMy>(&v1);//void printMy(T &my)

	// transform 使用函数适配器 和函数对象
	list mylist;
	mylist.resize(v1.size());
	transform(v1.begin(),v1.end(),v1.begin(),bind2nd(multiplies(),10));
	printList(mylist);//printMy>(mylist);//void printMy(T &my)

	// 也可以吧运算结果 直接输出到屏幕
	transform(v1.begin(),v1.end(),ostream_iterator(cout," "),negate());
}

// 常用的查找算法
// adjacent_find 在iterator对标识元素范围内,查找一对相邻重复元素,
// 找到则返回指向这对元素的第一个元素的迭代器

void main33()
{
	vector v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(5);

	vector::iterator it = adjacent_find(v1.begin(),v1.end());
	if (it == v1.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << *it << endl;
	}

	int index = distance(v1.begin(),it);
	cout << "index:" < v1;
	v1.push_back(1);
	v1.push_back(3);
	v1.push_back(5);
	v1.push_back(7);
	v1.push_back(9);

	bool b = binary_search(v1.begin(),v1.end(),7);
	if ( b == true)
	{
		cout << "找到" << endl;
	}
	else
	{
		cout << "没有找到" << endl;
	}
}
void main()
{
	//main31_foreach();
	//main32_transform();
	//main33();
	main34_binary_search();
	cout << "hello..."<< endl;

	system("pause");

}


你可能感兴趣的:(C/C++)