《C++STL标准程序库开发指南》第四章,C++中的算法

以有限的步骤解决逻辑或数学上的问题,称为算法。

算法大致分为:
		1  基本算法,
		2  数据结构的算法,
		3  数论与代数算法,
		4  计算几何算法,
		5  图论算法,
		6  动态规划
		7  数值分析,
		8  加密算法,
		9  排序算法,
		10 检索算法,
		11 随机化算法,
		12 并行算法
		13 等一系列算法
 
STL的算法库分为4种:
				1 非修改式序列算法
					一般用input迭代器和forward迭代器完成,用于所有的标准容器
				2 修改式序列算法
				3 排序和相关算法
				4 通用数字算法

1 非修改式序列算法

for_each算法
元素计数算法
最小值和最大值算法
搜寻算法
区间比较算法

for_each(Iterator begin ,Iterator end,proc op)
for_each算法实现对区间[begin,end]中每个元素均调用进程op

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template <class T>
void FillValue(T& vect,int first,int last)
{
	if(last >= first)
	{
		for(int i=first;i<=last;++i)
			vect.insert(vect.end(),i);
	}
	else
	{
		cout<<"The indexes is error:last < first."<<endl;
	}
}
void print(int elem)
{
	cout<<elem<<" ";
}
template <class T>
class Multiple{
private:
	T theValue;
public:
	Multiple(const T& v):theValue(v){}
	void operator()(T& elem)const
	{
		elem*=theValue;
	}
};
int main(int argc, char const *argv[])
{
	std::vector<int> myvector;
	FillValue(myvector,1,10);
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	for_each(myvector.begin(),myvector.end(),Multiple<int>(2));
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	return 0;
}
输出:
1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 

仿函数,又称函数对象,上述代码,利用仿函数改变容器中每个元素的值。所谓仿函数即,使一个类使用时,
像是在调用函数。实现时候,类中包含的是operator(),之后这个类就有了类似的函数行为,
就是一个仿函数类。
当使用类似函数调用形式 classname()时,函数operator()被自动执行。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template <class T>
void FillValue(T& vect,int first,int last)
{
	if(last >= first)
	{
		for(int i=first;i<=last;++i)
			vect.insert(vect.end(),i);
	}
	else
	{
		cout<<"The indexes is error:last < first."<<endl;
	}
}
void print(int elem)
{
	cout<<elem<<" ";
}
class SUM{
private:
	long sum_D;
public:
	SUM():sum_D(0){}
	void operator()(int elem)
	{
		sum_D+=elem;
	}
	operator double()
	{
		return static_cast<double>(sum_D);
	}
};

int main(int argc, char const *argv[])
{
	std::vector<int> myvector;
	FillValue(myvector,1,10);
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	double sum = for_each(myvector.begin(),myvector.end(),SUM());
	cout<<"The sum:"<<sum<<endl;
	return 0;
}
输出:
1 2 3 4 5 6 7 8 9 10 
The sum:55

元素计数算法

count(Iterator begin,Iterator end,const T& value);
count(Iterator begin,Iterator end,UnaryPredicate op);

#include<functional>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template <class T>
void FillValue(T& vect,int first,int last)
{
	if(last >= first)
	{
		for(int i=first;i<=last;i++)
			vect.insert(vect.end(),i);
	}
	else
		cout<<"The indexes is error: last < first."<<endl;
}
void print(int elem)
{
	cout<<elem<<" ";
}
bool isEven(int elem)
{
	return elem%2 == 0;
}
int main()
{
	vector <int> myvector;
	FillValue(myvector,1,10);
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	int ct = count(myvector.begin(),myvector.end(),4);
	int ctif = count_if(myvector.begin(),myvector.end(),isEven);
	//统计偶数个数
}
最大最小值算法
Iterator min_element(Iterator beg,Iterator end);
Iterator min_element(Iterator begin,Iterator end,compFunc op);

Iterator max_element(Iterator beg,Iterator end);
Iterator max_element(Iterator begin,Iterator end,compFunc op);

#include<functional>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template<class T>
void FillValue(T& vect,int first,int last)
{
	if(last >= first)
		for(int i=first;i<=last;++i)
			vect.insert(vect.end(),i);
	else
		cout<<"The indexes is error:last < first"<<endl;

}
void print(int elem)
{
	cout<<elem<<" ";
}

bool AbsLess(int elem,int elem2)
{
	return abs(elem)<abs(elem2);
}
int main(int argc, char const *argv[])
{
	std::vector<int> myvector;
	FillValue(myvector,-3,12);
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	cout<<"minimum:"<<*min_element(myvector.begin(),myvector.end())<<endl;
	cout<<"maximum:"<<*max_element(myvector.begin(),myvector.end())<<endl;
	cout<<"获得向量中绝对值最小最大的元素:"<<endl;
	cout<<"minimum of absolute value:"<<*min_element(myvector.begin(),myvector.end(),AbsLess)<<endl;
	cout<<"maximum of absolute value:"<<*max_element(myvector.begin(),myvector.end(),AbsLess)<<endl;
	return 0;
}
输出:
	-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 
	minimum:-3
	maximum:12
	获得向量中绝对值最大最小的元素:
	minimum of absolute value:0
	maximum of absolute value:12

搜素算法
第一组搜索算法函数(搜索第一个匹配元素)
Iterator find(Iterator begin,Iterator end,const T& value);
Iterator find_if(Iterator begin,Iterator end,UnaryPredicate op);

#include<functional>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template<class T>
void FillValue(T& vect,int first,int last)
{
	if(last >= first)
		for(int i=first;i<=last;++i)
			vect.insert(vect.end(),i);
	else
		cout<<"The indexes is error:last < first"<<endl;

}
void print(int elem)
{
	cout<<elem<<" ";
}

bool AbsLess(int elem,int elem2)
{
	return abs(elem)<abs(elem2);
}
int main(int argc, char const *argv[])
{
	std::vector<int> myvector;
	FillValue(myvector,-3,12);
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	vector<int>::iterator pos1;
	pos1 = find(myvector.begin(),myvector.end(),5);
	vector<int>::iterator pos2;
	pos2 = find_if(myvector.begin(),myvector.end(),bind2nd(greater<int>(),3));
	cout<<"第一个等于5的元素位置:"<<distance(myvector.begin(),pos1)+1<<endl;
	cout<<"第一个大于3的元素位置:"<<distance(myvector.begin(),pos2)+1<<endl;
	return 0;
}
输出:
	-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 
	第一个等于5的元素位置:9
	第一个大于3的元素位置:8

第二组搜索算法函数(搜索前n个连续匹配值)
Iterator search_n(Iterator begin,Iterator end,Size count,const T& value);
Iterator search_n(Iterator begin,Iterator end,Size count,const T& value,BinaryPredicate op);

第三组搜索算法函数(搜寻第一个子区间)
Iterator search(Iterator begin,Iterator end,Iterator2 searchbegin,Iterator2 searchend);
Iterator search(Iterator begin,Iterator end,Iterator2 searchbegin,Iterator2 searchend,BinaryPredicate op);

#include<functional>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template<class T>
void FillValue(T& vect,int first,int last)
{
	if(last >= first)
		for(int i=first;i<=last;++i)
			vect.insert(vect.end(),i);
	else
		cout<<"The indexes is error:last < first"<<endl;

}
void print(int elem)
{
	cout<<elem<<" ";
}
bool checkEven(int elem,bool even)
{
	if(even)
		return elem%2 == 0;
	else
		return elem%2 == 1;
}

int main(int argc, char const *argv[])
{
	std::vector<int> myvector;
	std::vector<int> subvector;
	bool checkEvenArg[3] = {true,false,true};
	FillValue(myvector,-3,12);
	FillValue(subvector,-1,3);
	for_each(myvector.begin(),myvector.end(),print);
	cout<<endl;
	for_each(subvector.begin(),subvector.end(),print);
	cout<<endl;
	vector<int>::iterator pos1;
	pos1 = search(myvector.begin(),myvector.end(),subvector.begin(),subvector.end());
	if(pos1!=myvector.end())
		cout<<"子串在原串中的位置"<<distance(myvector.begin(),pos1)+1<<endl;
	else
		cout<<"没有搜索到需要的子串"<<endl;
	vector<int>::iterator pos2;
	pos2 =  search(myvector.begin(),myvector.end(),checkEvenArg,checkEvenArg+3,checkEven);
	cout<<"满足‘偶,奇,偶’顺序的子串起始位置:"<<distance(myvector.begin(),pos2)+1<<endl;
	return 0;
}
输出:
	-3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 
	-1 0 1 2 3 
	子串在原串中的位置3
	满足‘偶,奇,偶’顺序的子串起始位置:44组搜索算法:(搜寻最后一个子区间)
Iterator find_end(Iterator begin,Iterator end,Iterator2 searchbegin,Iterator2 searchend,);
Iterator find_end(Iterator begin,Iterator end,Iterator2 searchbegin,Iterator2 searchend,BinaryPredicate op);5组搜索函数(搜寻某些元素的第一次出现位置)
Iterator find_first_of(	Iterator begin,Iterator end,Iterator2 searchbegin,Iterator2 search_end);
Iterator find_first_of(	Iterator begin,Iterator end,Iterator2 searchbegin,Iterator2 search_end,BinaryPredicate op);

vector<int>::reverse_iterator rpos;6组搜索函数(搜寻两个连续相等的元素)
template<class FwdIt> FwdIt adjacent_find(FwdIt first,FwdIt last);
template<class FwdIt,class Pred>FwdIt adjacent_find(FwdIt first,FwdIt last,Pred pr);

区间比较算法

equal()
mismatch()
lexicographical_compare()	属于字典式比较。

变动性算法

1 复制:copy() copy_backward()
2 转换: transform()
3 互换: swap()
4 赋值: fill() fill_n() generate() generate_n()
5 替换: replace() replace_if()
6 逆转: reverse() reverse_copy()
7 旋转: rotate() rotate_copy()
8 排列: 6种排列方式:
			1 排列元素:升序,降序
			2 重排元素:均匀分布,按指定规则
			3 将元素向前搬移:不排序,会保留一个相对的次序(排序)

排序

1 全部元素排序
		sort() 
		stable_sort()  
		(vector deque)
	
2 局部排序算法
		partial_sort() 
		partial_sort_copy()
		
3 根据某个元素排序
		nth_element()

堆(Heap)操作

"堆"的第一个元素通常具有最大值的元素
push_heap()
pop_heap()
sort_heap()
make_heap()

合并排序

merge()
set_union()
set_intersection()
set_differrnce()
inplace_merge()

搜索

binary_search()
includes()
lower_bound()
equal_range()
upper_bound()

删除算法

remove()
remove_if()
remove_copy()
remove_copy_if()
unique()
unique_copy()

总结:掌握STL的各种算法

发布了30 篇原创文章 · 获赞 5 · 访问量 2217

猜你喜欢

转载自blog.csdn.net/weixin_44408476/article/details/104904080