C++ stl 算法

目录

 

1.概述

2常用算法简介

2.1排序算法

2.2查找函数

2.3.删除和替换算法

2.4 排列组合算法

2.5.数值算法

2.6.生成和异变算法

2.7.关系算法

2.8.集合算法

2.9 堆算法


1.概述

对于C++的stl,除了一些基本结构和一些自带的函数,还有着大量的使用函数模板实现的算法支持stl。这些算法主要由<algorithm>,<numeric>组成。要使用stl的算法函数要包括头文件<algorithm>,对于数值算法要包含<numeric>

2常用算法简介

2.1排序算法

  • sort(),stable_sort()——对全体元素排序,不同的是stable_sort()会保证相等的元素的相对关系
  • partial_sort():对整个区间的元素进行排序,但只让[beg,sortend)有序
  • nth_element:将第n个元素排好序,即只将第n个元素放好位置,将小于第n个元素的元素放在其左边,将大于第n个元素的元素放在右边(不过结果看起来是已全部重新排序,和sort无区别???)
  • reverse:将容器中的元素反向存储(不可重载)
  • random_shuffle:对指定范围内的元素随机调整次序。
  • merge:合并两个有序序列,存放到另一个序列
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // 定义了greater<int>()

using namespace std;

// 要注意的技巧
template <class T>
struct display
{
	void operator()(const T&x) const
	{
		cout << x << " ";
	}
};

// 如果想从大到小排序,可以采用先排序后反转的方式,也可以采用下面方法:
// 自定义从大到小的比较器,用来改变排序方式
bool Comp(const int& a, const int& b) {
	return a > b;
}

int main(int argc, char* argv[])
{
	int iarr1[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 };
	vector<int> iv1(iarr1, iarr1 + sizeof(iarr1) / sizeof(int));
	vector<int> iv2(iarr1 + 4, iarr1 + 8); // 4 5 6 6
	vector<int> iv3(15);

	/*** merge: 合并两个有序序列,存放到另一个序列 ***/
	// iv1和iv2合并到iv3中(合并后会自动排序)
	merge(iv1.begin(), iv1.end(), iv2.begin(), iv2.end(), iv3.begin());
	cout << "merge合并后: ";
	for_each(iv3.begin(), iv3.end(), display<int>());
	cout << endl;

	/*** random_shuffle: 对指定范围内的元素随机调整次序。 ***/
	int iarr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
	vector<int> iv4(iarr2, iarr2 + sizeof(iarr2) / sizeof(int));
	// 打乱顺序  
	random_shuffle(iv4.begin(), iv4.end());
	cout << "random_shuffle打乱后: ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;

	/*** reverse: 将指定范围内元素重新反序排序。 ***/
	reverse(iv4.begin(), iv4.begin());
	cout << "reverse翻转后: ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;

	/*** partial_sort(): 对整个区间的元素进行排序,但只让[beg,sortend)有序。 ***/
	partial_sort(iv4.begin(), iv4.begin()+3, iv4.end());
	cout << "partial_sort排序: ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;

	random_shuffle(iv4.begin(), iv4.end());

	/*** nth_element: 将范围内的序列重新排序。 ***/
	// 将小于iv.begin+5的放到左边   
	nth_element(iv4.begin(), iv4.begin() + 5, iv4.end());
	cout << "nth_element重新排序后: ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;


	/*** sort: 以升序重新排列指定范围内的元素。 ***/
	// sort(iv4.begin(), iv4.end(), Comp); // 也可以使用自定义Comp()函数
	sort(iv4.begin(), iv4.end(), greater<int>());
	cout << "sort排序(倒序): ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;

	/*** stable_sort: 与sort类似,不过保留相等元素之间的顺序关系。 ***/
	int iarr3[] = { 0, 1, 2, 3, 3, 4, 4, 5, 6 };
	vector<int> iv5(iarr3, iarr3 + sizeof(iarr3) / sizeof(int));
	stable_sort(iv5.begin(), iv5.end(), greater<int>());
	cout << "stable_sort排序(倒序): ";
	for_each(iv5.begin(), iv5.end(), display<int>());
	cout << endl;

	return 0;
}

2.2查找函数

  • adjacent_find():返回范围内第一组相邻重复元素,,负责返回end()
  • count():统计范围内选定元素个数
  • count_if:统计范围内满足条件的元素个数
  • binary_search:在有序序列中查找元素
  • equal_range:返回一对迭代器,第一个表示lower_bound,第二个表示upper_bround
  • find():在范围内返回选定元素对应的额迭代器
  • find_if():在范围内返回
  • search():在范围内查找子序列的位置
  • search_n():在范围内查找子序列重复n次的位置
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> 

using namespace std;

int main(int argc, char* argv[])
{
	int iarr[] = { 0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8 };
	vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));

	/*** adjacent_find: 在iterator对标识元素范围内,查找一对相邻重复元素 ***/
	// 原型: _FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
	cout << "adjacent_find: ";
	cout << *adjacent_find(iv.begin(), iv.end()) << endl;

	/*** count: 利用等于操作符,把标志范围内的元素与输入值比较,返回相等元素个数。 ***/
	// 原型: count(_InIt _First, _InIt _Last, const _Ty& _Val)
	cout << "count(==6): ";
	cout << count(iv.begin(), iv.end(), 6) << endl;// 统计6的个数

	/*** count_if: 利用输入的操作符,对标志范围内的元素进行操作,返回结果为true的个数。 ***/
	// 原型: count_if(_InIt _First, _InIt _Last, _Pr _Pred)
	// 统计小于7的元素的个数 :9个
	cout << "count_if(<7): ";
	cout << count_if(iv.begin(), iv.end(), bind2nd(less<int>(), 7)) << endl;

	/*** binary_search: 在有序序列中查找value,找到返回true。 ***/
	// 原型: bool binary_search(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
	cout << "binary_search: ";
	cout << binary_search(iv.begin(), iv.end(), 4) << endl; // 找到返回true

	/*** equal_range: 功能类似equal,返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。 ***/
	// 原型: equal_range(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
	pair<vector<int>::iterator, vector<int>::iterator> pairIte;
	pairIte = equal_range(iv.begin(), iv.end(), 3);
	cout << "pairIte.first:" << *(pairIte.first) << endl;// lowerbound 3   
	cout << "pairIte.second:" << *(pairIte.second) << endl; // upperbound 4

	/*** find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。 ***/
	// 原型: _InIt find(_InIt _First, _InIt _Last, const _Ty& _Val)
	cout << "find: ";
	cout << *find(iv.begin(), iv.end(), 4) << endl; // 返回元素为4的元素的下标位置

	/*** find_if: 使用输入的函数代替等于操作符执行find。 ***/
	// 原型: _InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
	cout << "find_if: " << *find_if(iv.begin(), iv.end(), bind2nd(greater<int>(), 2)) << endl; // 返回大于2的第一个元素的位置:3 

	/*** search: 给出两个范围,返回一个ForwardIterator,查找成功指向第一个范围内第一次出现子序列的位置。 ***/
	// 原型: _FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1, _FwdIt2 _First2, _FwdIt2 _Last2)
	// 在iv中查找 子序列 2 3 第一次出现的位置的元素   
	int iarr3[3] = { 2, 3 };
	vector<int> iv3(iarr3, iarr3 + 2);
	cout << "search: " << *search(iv.begin(), iv.end(), iv3.begin(), iv3.end()) << endl;

	/*** search_n: 在指定范围内查找val出现n次的子序列。 ***/
	// 原型: _FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1, _Diff2 _Count, const _Ty& _Val)
	// 在iv中查找 2个6 出现的第一个位置的元素   
	cout << "search_n: " << *search_n(iv.begin(), iv.end(), 2, 6) << endl;

	return 0;
}

2.3.删除和替换算法

  • copy():复制序列
  • copy_backward():反向复制序列
  • iter_swap:交换两个迭代器的值
  • remove:删除指定范围内所有等于指定元素的元素
  • remove_copy:将所有不匹配的元素复制到一个指定的容器
  • remove_if:删除指定范围内输入操作结果为true的所有元素
  • remove_copy_if:将所有不匹配元素拷贝到一个指定容器
  • replace:将指定范围所有选定的元素替换成指定元素
  • replace_copy:将指定范围所有选定的元素替换成指定元素,并复制到一个指定的容器
  • replace_if:将指定范围内输入操作结果为true的所有元素替换成指定元素
  • replace_copy_if:将指定范围内输入操作结果为true的所有元素替换成指定元素,并复制到一个指定的容器
  • swap:交换存储在两个对象中的值
  • swap_range:在指定范围内的元素与另一个序列元素值进行交换
  • unique:清除序列中重复的元素
  • unique_copy:清除序列中重复的元素,并将结果输入到另外一个容器中
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional> // 定义了greater<int>()

using namespace std;

template <class T>
struct display
{
	void operator()(const T&x) const
	{
		cout << x << " ";
	}
};

int main(int argc, char* argv[])
{
	int iarr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
	vector<int> iv1(iarr1, iarr1 + sizeof(iarr1) / sizeof(int));
	vector<int> iv2(9);

	/*** copy: 复制序列 ***/
	//  原型: _OutIt copy(_InIt _First, _InIt _Last,_OutIt _Dest)
	copy(iv1.begin(), iv1.end(), iv2.begin());
	cout << "copy(iv2): ";
	for_each(iv2.begin(), iv2.end(), display<int>());
	cout << endl;

	/*** copy_backward: 与copy相同,不过元素是以相反顺序被拷贝。 ***/
	//  原型: _BidIt2 copy_backward(_BidIt1 _First, _BidIt1 _Last,_BidIt2 _Dest)
	copy_backward(iv1.begin(), iv1.end(), iv2.rend());
	cout << "copy_backward(iv2): ";
	for_each(iv2.begin(), iv2.end(), display<int>());
	cout << endl;

	/*** iter_swap:交换两个迭代器的值***/
	// 原型:void iter_swap(_FwdIt1 _Left, _FwdIt2 _Right)
	iter_swap(iv2.begin(), iv2.end()-1);
	cout << "iter_swap(iv2.begin(), iv2.end()):";
	for_each(iv2.begin(), iv2.end(), display<int>());
	cout << endl;

	/*** remove: 删除指定范围内所有等于指定元素的元素。 ***/
	//  原型: _FwdIt remove(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)
	remove(iv1.begin(), iv1.end(), 5); // 删除元素5
	cout << "remove(iv1): ";
	for_each(iv1.begin(), iv1.end(), display<int>());
	cout << endl;

	/*** remove_copy: 将所有不匹配元素复制到一个制定容器,返回OutputIterator指向被拷贝的末元素的下一个位置。 ***/
	//  原型: 	_OutIt remove_copy(_InIt _First, _InIt _Last,_OutIt _Dest, const _Ty& _Val)
	vector<int> iv3(8);
	remove_copy(iv1.begin(), iv1.end(), iv3.begin(), 4); // 去除4 然后将一个容器的元素复制到另一个容器
	cout << "remove_copy(iv3): ";
	for_each(iv3.begin(), iv3.end(), display<int>());
	cout << endl;

	/*** remove_if: 删除指定范围内输入操作结果为true的所有元素。 ***/
	//  原型: _FwdIt remove_if(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
	remove_if(iv3.begin(), iv3.end(), bind2nd(less<int>(), 6)); //  将小于6的元素 "删除"
	cout << "remove_if(iv3): ";
	for_each(iv3.begin(), iv3.end(), display<int>());
	cout << endl;

	/*** remove_copy_if: 将所有不匹配元素拷贝到一个指定容器。 ***/
	// 原型: _OutIt remove_copy_if(_InIt _First, _InIt _Last,_OutIt _Dest, _Pr _Pred)
	//  将iv1中小于6的元素 "删除"后,剩下的元素再复制给iv3
	remove_copy_if(iv1.begin(), iv1.end(), iv2.begin(), bind2nd(less<int>(), 4));
	cout << "remove_if(iv2): ";
	for_each(iv2.begin(), iv2.end(), display<int>());
	cout << endl;

	/*** replace:将指定范围所有选定的元素替换成指定元素 ***/
	// 原型:void replace(_FwdIt _First, _FwdIt _Last,const _Ty& _Oldval, const _Ty& _Newval)
	replace(iv2.begin(), iv2.end(), 8, 5);
	cout << "replace(iv2.begin(), iv2.end(), 8, 5): ";
	for_each(iv2.begin(), iv2.end(), display<int>());
	cout << endl;

	/*** replace_copy:将指定范围所有选定的元素替换成指定元素,并复制到一个指定的容器 ***/
	// 原型:_OutIt replace_copy(_InIt _First, _InIt _Last,	_OutIt _Dest, const _Ty& _Oldval, const _Ty& _Newval):
	vector<int> iv4(8);
	replace_copy(iv3.begin(), iv3.end(), iv4.begin(), 8, 5);
	cout << "replace_copy(iv3.begin(), iv3.end(), iv4.begin(), 8, 5): ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;

	/*** replace_if:将指定范围内输入操作结果为true的所有元素替换成指定元素 ***/
	// 原型:_OutIt replace_copy(_InIt _First, _InIt _Last,	_OutIt _Dest, const _Ty& _Oldval, const _Ty& _Newval):
	replace_if(iv3.begin(), iv3.end(), bind2nd(less<int>(), 7), 8);
	cout << "replace_if(iv3.begin(), iv3.end(), bind2nd(less<int>(), 7), 8): ";
	for_each(iv3.begin(), iv3.end(), display<int>());
	cout << endl;

	/***  replace_copy_if:将指定范围内输入操作结果为true的所有元素替换成指定元素,并复制到一个指定的容器 ***/
	// 原型:_OutIt replace_copy_if(_InIt _First, _InIt _Last,_OutIt _Dest, _Pr _Pred, const _Ty& _Val):
	replace_copy_if(iv3.begin(), iv3.end(), iv4.begin(), bind2nd(less<int>(), 8), 9);
	cout << "replace_copy_if(iv3.begin(), iv3.end(), iv4.begin(), bind2nd(less<int>(), 8), 9): ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;

	/*** swap:交换存储在两个对象中的值 ***/
	// 原型:void swap(vector<_Ty, _Alloc>& _Left, vector<_Ty, _Alloc>& _Right)
	swap(iv4, iv3);
	cout << "swap(iv4, iv3):iv4: ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << " iv3: ";
	for_each(iv3.begin(), iv3.end(), display<int>());
	cout << endl;

	/*** swap_range:在指定范围内的元素与另一个序列元素值进行交换 ***/
	// 原型:swap_ranges(iv4.begin()+2,iv4.end()-2,iv2.begin()+2)
	swap_ranges(iv4.begin()+2,iv4.end()-2,iv2.begin()+2);
	cout << "swap_ranges(iv4.begin()+2,iv4.end()-2,iv2.begin()+2):iv4: ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << " iv2: ";
	for_each(iv2.begin(), iv2.end(), display<int>());
	cout << endl;

	/*** unique:清除序列中重复的元素 ***/
	// 原型:_FwdIt unique(_FwdIt _First, _FwdIt _Last)
	unique(iv4.begin(), iv4.end());
	cout << "unique(iv4.begin(), iv4.end()): ";
	for_each(iv4.begin(), iv4.end(), display<int>());
	cout << endl;;
	/*** unique_copy:清除序列中重复的元素,并将结果输入到另外一个容器中 ***/
	unique_copy(iv4.begin(), iv4.end(),iv3.begin());
	cout << "unique_copy(iv4.begin(), iv4.end(),iv3.begin()): ";
	for_each(iv3.begin(), iv3.end(), display<int>());
	cout << endl;

	return 0;
}

2.4 排列组合算法

  • next_permutation:给出字典顺序排序的下一个,例如abc->acb->bac->bca->cab->cba中选定字符串的下一个
  • prev_permutation:给出字典顺序排序的上一个
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template <class T>
struct display
{
	void operator()(const T&x) const
	{
		cout << x << " ";
	}
};

int main(int argc, char* argv[])
{
	int iarr[] = { 12, 17, 20, 22, 23, 30, 33, 40 };
	vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));

	/*** next_permutation: 取出当前范围内的排列,并重新排序为下一个字典序排列。***/
	//  原型: bool next_permutation(_BidIt _First, _BidIt _Last)
	// 生成下一个排列组合(字典序)   
	next_permutation(iv.begin(), iv.end());
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;
	next_permutation(iv.begin(), iv.end());
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;


	return 0;
}

2.5.数值算法

  • accumulate:将容器中的元素之和加到一个初始值上
  • partial_sum:创建一个新序列,其中每个元素代表该位置至指定开头之间元素之和
  • inner_product:对两个序列做内积
  • adjacent_difference:创建一个新序列,每个元素代表当前元素与上一个元素之差
#include <iostream>
#include <vector>
#include <numeric> // 数值算法
#include <iterator> // 定义了ostream_iterator

using namespace std;

int main(int argc, char* argv[])
{
	int arr[] = { 1, 2, 3, 4, 5 };
	vector<int> vec(arr, arr + 5);
	vector<int> vec2(arr, arr + 5);

	//  accumulate: iterator对标识的序列段元素之和,加到一个由val指定的初始值上。
	int temp;
	int val = 1;
	temp = accumulate(vec.begin(), vec.end(), val);
	cout << "accumulate(val = 1): " << temp << endl;

	// inner_product: 对两个序列做内积(对应元素相乘,再求和)并将内积加到一个输入的初始值上。
	// 这里是:1*1 + 2*2 + 3*3 + 4*4 + 5*5
	val = 0;
	temp = inner_product(vec.begin(), vec.end(), vec2.begin(), val);
	cout << "inner_product(val = 0): " << temp << endl;

	// partial_sum: 创建一个新序列,其中每个元素值代表指定范围内该位置前所有元素之和。
	// 第一次,1   第二次,1+2  第三次,1+2+3  第四次,1+2+3+4
	ostream_iterator<int> oit(cout, " "); // 迭代器绑定到cout上作为输出使用
	cout << "ostream_iterator: ";
	partial_sum(vec.begin(), vec.end(), oit);// 依次输出前n个数的和
	cout << endl;
	// 第一次,1   第二次,1-2  第三次,1-2-3  第四次,1-2-3-4
	cout << "ostream_iterator(minus): ";
	partial_sum(vec.begin(), vec.end(), oit, minus<int>());// 依次输出第一个数减去(除第一个数外到当前数的和)
	cout << endl;

	// adjacent_difference: 创建一个新序列,新序列中每个新值代表当前元素与上一个元素的差。
	// 第一次,1-0   第二次,2-1  第三次,3-2  第四次,4-3
	cout << "adjacent_difference: ";
	adjacent_difference(vec.begin(), vec.end(), oit); // 输出相邻元素差值 后面-前面
	cout << endl;
	// 第一次,1+0   第二次,2+1  第三次,3+2  第四次,4+3
	cout << "adjacent_difference(plus): ";
	adjacent_difference(vec.begin(), vec.end(), oit, plus<int>()); // 输出相邻元素差值 后面-前面 
	cout << endl;

	return 0;
}

2.6.生成和异变算法

  • fill:将输入值赋给标志范围内的元素
  • fill_n:将输入值赋给first到first+n范围内的所有元素
  • for_each:用指定函数依次对指定范围内所有元素进行迭代访问
  • generate: 连续调用输入的函数来填充指定的范围
  • generate_n: 与generate函数类似,填充从指定iterator开始的n个元素。
  • transform: 将输入的操作作用与指定范围内的每个元素,并产生一个新的序列
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

template <class T>
struct display
{
	void operator()(const T&x) const
	{
		cout << x << " ";
	}
};
//  作用类似于上面结构体,只不过只能显示int类型的数据
void printElem(int& elem)
{
	cout << elem << " ";
}

template<class T>
struct plus2
{
	void operator()(T&x)const
	{
		x += 2;
	}

};

class even_by_two
{
private:
	static int _x; //  注意静态变量   
public:
	int operator()()const
	{
		return _x += 2;
	}
};
int even_by_two::_x = 0; //  初始化静态变量

int main(int argc, char* argv[])
{
	int iarr[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
	vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));

	/*** fill: 将输入值赋给标志范围内的所有元素。 ***/
	//  原型: void fill(_FwdIt _First, _FwdIt _Last, const _Ty& _Val)  
	fill(iv.begin(), iv.end(), 5);
	cout << "fill: ";
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;

	/*** fill_n: 将输入值赋给first到first+n范围内的所有元素。 ***/
	//  原型: _OutIt fill_n(_OutIt _Dest, _Diff _Count, const _Ty& _Val)
	fill_n(iv.begin(), 4, 3); //  赋4个3给iv 
	cout << "fill_n: ";
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;

	/*** for_each: 用指定函数依次对指定范围内所有元素进行迭代访问,返回所指定的函数类型。 ***/
	//  原型: _Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
	for_each(iv.begin(), iv.end(), plus2<int>()); //  每个元素+2
	cout << "for_each: ";
	for_each(iv.begin(), iv.end(), printElem); //  输出
	cout << endl;

	/*** generate: 连续调用输入的函数来填充指定的范围。 ***/
	//  原型: void generate(_FwdIt _First, _FwdIt _Last, _Fn0 _Func)
	//  使用even_by_two()函数返回的值,来填充容器iv
	generate(iv.begin(), iv.end(), even_by_two());
	cout << "generate: ";
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;

	/*** generate_n: 与generate函数类似,填充从指定iterator开始的n个元素。 ***/
	//  原型: _OutIt generate_n(_OutIt _Dest, _Diff _Count, _Fn0 _Func)
	//  使用even_by_two()函数返回的值,来填充容器iv的前三个值
	generate_n(iv.begin(), 3, even_by_two());
	cout << "generate_n: ";
	for_each(iv.begin(), iv.end(), display<int>()); //  由于_X是static 所以接着 增长
	cout << endl;

	/*** transform: 将输入的操作作用与指定范围内的每个元素,并产生一个新的序列。 ***/
	//  原型: _OutIt transform(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn1 _Func)
	// 容器的所有值全部减2
	transform(iv.begin(), iv.end(), iv.begin(), bind2nd(minus<int>(), 2));
	cout << "transform: ";
	for_each(iv.begin(), iv.end(), display<int>()); //  由于_X是static 所以接着 增长
	cout << endl;

	return 0;
}

2.7.关系算法

  • equal: 如果两个序列在标志范围内元素都相等,返回true。
  • includes: 判断第一个指定范围内的所有元素是否都被第二个范围包含,使用底层元素的<操作符,成功返回true。重载版本使用用户输入的函数。
  • max: 返回两个元素中较大一个。重载版本使用自定义比较操作。
  • min: 返回两个元素中较小一个。重载版本使用自定义比较操作。
  • max_element: 返回一个ForwardIterator,指出序列中最大的元素。重载版本使用自定义比较操作。
  • min_element: 返回一个ForwardIterator,指出序列中最小的元素。重载版本使用自定义比较操作。
  • mismatch: 并行比较两个序列,指出第一个不匹配的位置,返回一对iterator,标志第一个不匹配元素位置。如果都匹配,返回每个容器的last。重载版本使用自定义的比较操作。
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(int argc, char* argv[])
{
	int iarr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	vector<int> iv1(iarr, iarr + 5);
	vector<int> iv2(iarr, iarr + 9);

	//  equal: 如果两个序列在标志范围内元素都相等,返回true。
	cout << "equal: " << equal(iv1.begin(), iv1.end(), iv2.begin()) << endl;//  1 表示相等,因为只比较跟 iv1长度大小的数组      

	// includes: 判断第一个指定范围内的所有元素是否都被第二个范围包含,使用底层元素的<操作符,成功返回true。
	// 判断判断iv2中元素是否都出现在iv1中
	cout << "includes: " << includes(iv1.begin(), iv1.end(), iv2.begin(), iv2.end()) << endl;

	// max: 返回两个元素中较大一个。
	cout << "max: " << max(iv1[0], iv1[1]) << endl;
	// min: 返回两个元素中较小一个。
	cout << "min: " << min(iv1[0], iv1[1]) << endl;

	// max_element: 返回一个ForwardIterator,指出序列中最大的元素。
	cout << "max_element: " << *max_element(iv1.begin(), iv1.end()) << endl;
	// min_element: 返回一个ForwardIterator,指出序列中最小的元素。
	cout << "min_element: " << *min_element(iv1.begin(), iv1.end()) << endl;

	//  mismatch: 并行比较两个序列,指出第一个不匹配的位置,返回一对iterator,标志第一个不匹配元素位置。如果都匹配,返回每个容器的last。
	pair<vector<int>::iterator, vector<int>::iterator> pa;
	pa = mismatch(iv1.begin(), iv1.end(), iv2.begin());
	if (pa.first == iv1.end()) //  true 表示相等,因为只比较跟iv1长度大小的数组 
		cout << "第一个向量与第二个向量匹配" << endl;
	else
	{
		cout << "两个向量不同点--第一个向量点:" << *(pa.first) << endl; // 这样写很危险,应该判断是否到达end   
		cout << "两个向量不同点--第二个向量点:" << *(pa.second) << endl;
	}

	return 0;
}

2.8.集合算法

  • set_union: 构造一个有序序列,包含两个序列中所有的不重复元素。重载版本使用自定义的比较操作。
  • set_intersection: 构造一个有序序列,其中元素在两个序列中都存在。重载版本使用自定义的比较操作。
  • set_difference: 构造一个有序序列,该序列仅保留第一个序列中存在的而第二个中不存在的元素。重载版本使用自定义的比较操作。
  • set_symmetric_difference: 构造一个有序序列,该序列取两个序列的对称差集(并集-交集)。
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator> 

using namespace std;

template <class T>
struct display
{
	void operator()(const T&x) const
	{
		cout << x << " ";
	}
};

int main(int argc, char* argv[])
{
	int iarr1[] = { 1, 3, 5, 7, 9, 11 };
	int iarr2[] = { 1, 1, 2, 3, 5, 8, 13 };

	multiset<int> s1(iarr1, iarr1 + 6);
	multiset<int> s2(iarr2, iarr2 + 7);
	cout << "s1: ";
	for_each(s1.begin(), s1.end(), display<int>());
	cout << endl;
	cout << "s2: ";
	for_each(s2.begin(), s2.end(), display<int>());
	cout << endl;

	/*** set_union: 构造一个有序序列,包含两个序列中所有的不重复元素。 ***/
	//  原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
	cout << "union of s1 and s2: ";
	// 两个集合合并,相同元素个数取 max(m,n)。   
	set_union(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	/*** set_intersection: 构造一个有序序列,其中元素在两个序列中都存在。 ***/
	//  原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
	cout << "Intersection of s1 and s2: ";
	// 两个集合交集,相同元素个数取 min(m,n).  
	set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	/*** set_difference: 构造一个有序序列,该序列仅保留第一个序列中存在的而第二个中不存在的元素。 ***/
	//  原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
	cout << "Intersection of s1 and s2: ";
	// 两个集合差集 就是去掉S1中 的s2   
	set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	/*** set_symmetric_difference: 构造一个有序序列,该序列取两个序列的对称差集(并集-交集)。 ***/
	//  原型: _OutIt set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest)
	cout << "Intersection of s1 and s2: ";
	// 两个集合对称差集:就是取两个集合互相没有的元素 。两个排序区间,元素相等指针后移,不等输出小的并前进   
	// 相同元素的个数 abs(m-n)   
	set_symmetric_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), ostream_iterator<int>(cout, " "));
	cout << endl;

	return 0;
}

2.9 堆算法

  • make_heap: 把指定范围内的元素生成一个堆。保证最大值在所给范围的最前面,其他值的位置不确定,重载版本使用自定义比较操作。
  • pop_heap: 将堆顶(所给范围的最前面)元素移动到所给范围的最后,并且将新的最大值置于所给范围的最前面
  • push_heap: 假设first到last-1是一个有效堆,要被加入到堆的元素存放在位置last-1,重新生成堆。在指向该函数前,必须先把元素插入容器后。重载版本使用指定的比较操作。
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

template <class T>
struct display
{
	void operator()(const T&x) const
	{
		cout << x << " ";
	}
};

int main(int argc, char* argv[])
{
	int iarr[] = { 9,5, 1, 7, 8 };
	vector<int> iv(iarr, iarr + sizeof(iarr) / sizeof(int));

	/*** make_heap: 把指定范围内的元素生成一个堆。 ***/
	//  原型: void make_heap(_RanIt _First, _RanIt _Last)
	make_heap(iv.begin(), iv.end());
	cout << "make_heap: ";
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;

	/*** pop_heap: 并不真正把最大元素从堆中弹出,而是重新排序堆。 ***/
	//  原型: void pop_heap(_RanIt _First, _RanIt _Last)
	pop_heap(iv.begin(), iv.end());
	iv.pop_back();
	cout << "pop_heap: ";
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;

	/*** push_heap: 假设first到last-1是一个有效堆,要被加入到堆的元素存放在位置last-1,重新生成堆。 ***/
	//  原型: void push_heap(_RanIt _First, _RanIt _Last)
	iv.push_back(6);
	push_heap(iv.begin(), iv.end());
	cout << "push_heap: ";
	for_each(iv.begin(), iv.end(), display<int>());
	cout << endl;

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值