STL非修改算法

 

由于STL算法都是通过迭代器间接处理容器,下面定义istream_iteratorInIt,ostream_itreatorOutIt,forward_iteratorFwdIt,bidirectional_iterator BidIt,random_iterator RanIt

非修改算法:

算法

用法说明

adjacent_find

FwdIt adjacent_find(FwdIt first,FwdIt last);

FwdIt adjacent_find(FwdIt first,FwdIt last,Pred pr);

在[first,last)查找相同元素的首次出现或能使pr(elem,nextElem)为true的元素的位置 ,函数查找成功返回位置,失败返回last

binary_search

bool binary_search(FwdIt first,FwdIt last,const T& val);

bool binary_search(FwdIt first,FwdIt last,const T& val,Pred pr);

在区间[first,last)中查找元素val,如果找到返回true,否则返回false,第二种形式pr用于设定查找准则

count

size_t count(InIt first,InIt last,const T& val);

返回区间[first,last)上val出现的次数

count_if

size_t count_if(InIt first,InIt last,Pred pr);

返回区间[first,last)上满足条件pr(elem)的元素个数

equal

bool equal(InIt1 first,InIt1 last,InIt2 x);

bool equal(InIt1 first,InIt1 last,InIt2 x,Pred pr);

判断[first,last)与x开始的区间的元素是否相等,pr用于指定判断函数

equal

pair<FwdIt,FwdIt> equal_range(FwdIt first,FwdIt last,const T& val);

pair<FwdIt,FwdIt> equal_range(FwdIt first,FwdIt last,const T& val,Pred pr);

返回元素val第一次出现的位置和最后出现的位置的下一位组成的对,pr指定比较算法

lower_bound

FwdIt lower_bound(FwdIt first,FwdIt last,const T& val);

FwdIt lower_bound(FwdIt first,FwdIt last,const T& val,Pred pr);

返回已排序序列[first,last)中val首次出现的位置,pr指定比较算法

upper_bound

FwdIt upper_bound(FwdIt first,FwdIt last,const T& val);

FwdIt upper_bound(FwdIt first,FwdIt last,const T& val,Pred pr);

返回已排序序列[first,last)中val最后一次出现的下一个位置,pr指定比较算法

find

InIt find(InIt first,InIt last,const T& val);

在[first,last)之间查找元素val,如果找到返回位置,找不到返回last

find_if

InIt find_if(InIt first,InIt last, Pred pr);

在[first,last)之间查找能使函数pr返回true的元素,找到返回位置,否则返回last

find_end

FwdIt1 find_end(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2);

FwdIt1 find_end(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2, Pred pr);

在[first1,last1)之间查找[first2,last2)最后出现的位置,如果找到返回位置,失败返回last1,第二个函数的pr函数用于比较两个容器的元素,在两个容器的元素相等时返回true

find_first_of

FwdIt1 find_first_of(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2);

FwdIt1 find_first_of(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2, Pred pr);

在[first1,last1)之间查找第一次出现[first2,last2)中元素的位置,找到返回位置,失败返回last1,第二个函数pr用于比较两个容器的元素是否相等

for_each

Fun for_each(InIt first,InIt last, Fun f);

对[first,last)上的所有元素执行函数f(elem),返回值常被忽略

includes

bool includes(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2);

bool includes(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2, Pred pr);

判断已排序序列[first1,last1)中是否包含区间已排序区间[first2,last2),pr指定元素的顺序

mismatch

pair<InIt1,InIt2> mismatch(InIt1 first,InIt1 last,InIt2 x);

pair<InIt1,InIt2> mismatch(InIt1 first,InIt1 last,InIt2 x, Pred pr);

返回序列[first,last)与x开始的序列第一个不匹配的位置的两个迭代器组成的对

max

const T& max(const T& x,const T& y);

const T& max(const T& x,const T& y, Pred pr);

返回x,y之间的较大者,pr(elem1,elem2)用于指定比较规则

max_element

FwdIt max_element(FwdIt first,FwdIt last);

FwdIt max_element(FwdIt first,FwdIt last, Pred pr);

返回区间[first,last)上最大值的位置,pr(elem1,elem2)用于指定比较规则

min

const T& min(const T& x,const T& y);

const T& min(const T& x,const T& y, Pred pr);

返回x,y之间的较小者,pr(elem1,elem2)用于指定比较规则

min_element

FwdIt min_element(FwdIt first,FwdIt last);

FwdIt min_element(FwdIt first,FwdIt last, Pred pr);

返回区间[first,last)上的最小值的位置,pr(elem1,elem2)用于指定比较规则

search

FwdIt1 search(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2);

FwdIt1 search(FwdIt1 first1,FwdIt1 last1,FwdIt2 first2,FwdIt2 last2, Pred pr);

在[first1,last1)中查找子区间[first2,last2),如果找到返回在第一个区间中的位置,失败返回last1,第二种形式pr函数用于设定比较函数

search_n

FwdIt search_n(FwdIt first,FwdIt last,Dist n,const T& val);

FwdIt search_n(FwdIt first,FwdIt last,Dist n,const T& val, Pred pr);

在[first,last)中查找连续n个val,如果找到返回在区间中的位置,失败返回last,第二种形式pr用于设定比较函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值