【C++研发面试笔记】21. 常用算法-STL中常用算法函数

【C++研发面试笔记】21. 常用算法-STL中常用算法函数

1、for_each(容器起始地址,容器结束地址,要执行的函数)
指定函数依次对指定范围内所有元素进行迭代访问,返回所指定的函数类型。

2、查找find
InputIterator find (InputIterator first, InputIterator last, const T& val);
前闭后合的区间 begin,end中,查找value如果查找到了就返回第一个符合条件的元素,否则返回end指针

3、数值运算(<numeric>
T accumulate (InputIterator first, InputIterator last, T init)
计算一个范围内的累积和
OutputIterator adjacent_difference (InputIterator first, InputIterator last, OutputIterator result)
计算相邻差(y0=x0, y1=x1-x2, y2=x2-x1…)
T inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, T init)
计算内积(y=init+x0*x0+x1*x1+…)

4、判断是否相等
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )

5、查找
ForwardIterator1 search ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2) 找到所示子序列的位置

6、复制
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)

7、排序
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
cmp函数的两种使用方式
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;

std::sort (myvector.begin()+4, myvector.end(), myfunction);
std::sort (myvector.begin(), myvector.end(), myobject);

8、交换
template <class T> void swap ( T& a, T& b )

9、取代
void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value)

10、反转
void reverse (BidirectionalIterator first, BidirectionalIterator last)

11、统计元素值为val的数目
difference_type count (InputIterator first, InputIterator last, const T& val)

12、最值比较
const T& min (const T& a, const T& b)
const T& max (const T& a, const T& b)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值