泛型算法简要

来源:互联网 发布:穿越火线网络环境异常 编辑:程序博客网 时间:2024/05/17 04:16
泛型算法简要
需要#include <algorithm>和#include <numeric>头文件
第一  find()函数 
1 容器元素查找
list<int>iList;
int iFindVal=11;
list<int>::const_iterator iter=find(iList.begin(),iList.end(),0);
if(iter==iList.end)
  //没有该元素
else //找到该元素


2 数组及指针数组内的查找
int iArray[]={1,1,1,1,1};
int iFindVal=1;
int* iResult=find(iArray,iArray+5,iFindVal);
if(iResult==iArray+5)//没有找到!
elas//找到


find 的功能主要是给定一个范围去查找需要的值!


第二 accumulate()
vector<int>ivec;
int sum=accumulate(ivec.begin(),ivec.end(),4);
表示把容器在规定的范围内得元素累加后加4;


vector<string >ivec;
string strSum=accumulate(ivec.begin(),ivec.end(),string(""));
表示把string 类型得容器所有元素组合起来


第三 find_first_of()
list<string>strName1,strName2;
size_t iSame=0;
list<string>::iterator iter=strName1.begin();
while((iter=find_first_of(iter,strName1.end(),strName2.begin(),strName2.end()))!=strName1.end())
{
++iSame;
++iter;
}
查询2个容器中有没有相同的元素


第四 fill()
list<int>iList;
fill(iList.begin(),iList.end(),10);
把容器值全部写为10;


第五 fill_n()
list<int>iList;
fill_n(iList.begin(),10,10);
把容器值全部写为10;
注意:fill()和fill_n()都是把容器等一定范围内改写其值,使用此函数的前提为不为空或不越界!


第六 为解决以上2问题引入back_inserter()
list<int>iList;
back_inserter(iList.begin(),10,10);


第七 copy()
list<int>iList;
vector<int>iVec;
copy(iList.begin(),iList,end(),back_inserter(iVec));
复制元素


第八 replace()
list<int>iList;
replace(iList.begin(),iList.end(),0,42);
前2个参数为起点和终点,第三个参数为需要改变的值,第四参数为最终改变的值
//将iList所有元素为0的改为42;


第九 replace_copy()
list<int>iList;
vector<int>iVec; 
replace_copy(iList.begin(),iList.end(),back_inserter(iVec),0,42);
该函数iList没有改变,iVec存的是一个副本,同时在存入的同时把iList里的0改为42后写入iVec;


第十 sort(),unique()
list<int>iList;
sort(iList.begin(),iList.end());//排序默认为<号
//相当于是sort(iList.begin(),iList.end(),less<int>());
list<int>::iterator iter =unique(iList.begin(),iList.end());
去除重叠的内容但并没有删除
iList.erase(iter,iList.end());


第十一  stable_sort()
list<string>sList;
bool isSort(const string& s1,const string &s2)
{
return s1.size()<s2.size();
}
stable_sort(sList.begin(),sList.end(),isSort);
//自定义的排序
第十二 count_if()自定义查找
bool isFind(const string &s)
{
return s.size()>=6;
}
list<string>::size_type it=count_if(sList.begin(),sList.end(),isFind);
//以上是查找长度小于6的字符串




////////////////////////////
以下是所有STL sort算法函数的名字列表:
函数名     功能描述
sort     对给定区间所有元素进行排序
stable_sort     对给定区间所有元素进行稳定排序
partial_sort     对给定区间所有元素部分排序 采用锥排序
partial_sort_copy     对给定区间复制并排序 实际是copy和partial_sort 也可是copy与sort的组合
nth_element     找出给定区间的某个位置对应的元素
is_sorted     判断一个区间是否已经排好序


partition     使得符合某个条件的元素放在前面
stable_partition     相对稳定的使得符合某个条件的元素放在前面
其中nth_element 是最不易理解的,实际上,这个函数是用来找出第几个。例如:找出包含7个元素的数组中排在中间那个数的值,此时,我可能不关心前面,也不关心后面,我只关心排在第四位的元素值是多少。


partial_sort(iList.begin(),iList.begin()+5,less<int>());//排序取前五




//===================
效率由高到低(耗时由小变大):


   1. partion
   2. stable_partition
   3. nth_element
   4. partial_sort
   5. sort
   6. stable_sort 


///=======================
* 若需对vector, string, deque, 或 array容器进行全排序,你可选择sort或stable_sort;
    * 若只需对vector, string, deque, 或 array容器中取得top n的元素,部分排序partial_sort是首选.
    * 若对于vector, string, deque, 或array容器,你需要找到第n个位置的元素或者你需要得到top n且不关系top n中的内部顺序,nth_element是最理想的;
    * 若你需要从标准序列容器或者array中把满足某个条件或者不满足某个条件的元素分开,你最好使用partition或stable_partition;
    * 若使用的list容器,你可以直接使用partition和stable_partition算法,你可以使用list::sort代替sort和stable_sort排序。若你需要得到partial_sort或nth_element的排序效果,你必须间接使用。正如上面介绍的有几种方式可以选择。