STL算法

STL有大量用来处理容器的算法。这些算法可以分为如下几类:排序和搜索、数值处理、集合运算、拷贝等。
1、reverse
template<class BidirectionalIterator>
void reverse( BidrectionalIterator it1,
BidrectionalIterator it2);

需要注意的是,reverse的两个参数均为双向迭代器类型。 

2、generate

eg. Generate(v.begin(),v.end(),rand);

 3、replace_if

bool odd(int i){return i%2!=0;}
replace_if( v.begin(),v.end(), odd,0);

 4、count_if

 5、排序

  5.1 sort    

bool bomp(cons int& i1,const int& i2){return i1>i2;}

sort(it1,it2,bomp);
sort(it1,it2) //使用operator<
  5.2 stable_sort
sort()和stable_sort()都對container做sort的動作,但對於相等的值,sort()和stable_sort()處理的方式不一樣,stable_sort()會保證不更改原先的順序,但sort()則不保證,有可能更改順序,但也有可能不改。sort效率比stable_sort高。
 
原文地址:https://www.cnblogs.com/hbt19860104/p/2626612.html