stl 常用算法

for_each

template<typename Collection,typename Func>
void for_each(Collection const &collection,Func func)
{
	std::for_each(collection.begin(),collection.end(),func);
}

map

template <typename Collection,typename unop>
Collection map(Collection col,unop op) 
{
	std::transform(col.begin(),col.end(),col.begin(),op);
	return col;
}

reduce

//transform实现reduce
//这里的参数是复制过来的,所以在函数里面修改col容器,并不影响原始数据;
template <typename Collection,typename binop>
auto reduce(Collection col,binop op) 
{
	//Collection result(col.size());
	std::transform(col.begin(),col.end()-1,col.begin()+1,col.begin()+1,op);
	return *(col.end()-1);
}

exists

filter

最后更新于

这有帮助吗?