C++ : STL常用算法: inner_product , sort ,itoa

目录

1.std::count

2.std::inner_product

3.atoi

4.itoa

5 is_sorted      

6  sort 

7. fill

8 mismatch


1.std::count

count(_InputIterator __first, _InputIterator __last, const _Tp& __value)

在头文件algorithm 中,用来记录线性表 从 __first 到 __last   中出现  __value的次数 

#include<algorithm>
#include<iostream>
using namespace std;
int main(){
   int a[10] ={9,34,24,56,31,24,66,3,45,98};
   cout<< std::count(a,a+10,24)<<endl;    //输出2
}

2.std::inner_product

template<typename _InputIterator1, typename _InputIterator2, typename _Tp,
  typename _BinaryOperation1, typename _BinaryOperation2>
  inline _Tp
  inner_product(_InputIterator1 __first1, _InputIterator1 __last1,
  _InputIterator2 __first2, _Tp __init,
  _BinaryOperation1 __binary_op1,
  _BinaryOperation2 __binary_op2)

其中  __binary_op1  和__binary_op2  类似与算术运算符  ,也可以是一个两个参数的自定义函数。

__first1:代表线性表的起始位置

__last1: 代表线性表的结束位置

__first2: 代表另一个线性表的起始为位置

__init : 代表初始值

__binary_op1: 两个相之间的算术符 ,默认是  加法

__binary_op2  : 两个线性表元素的算术符   默认是乘法

函数作用: 返回和     __init数据类型相同的数   

  ret=  init  op1   (*(first1++ )  op2   * (first2++))        first1< last1 ;

例如

#include<algorithm>
#include<iostream>
using namespace std;
int main(){
   int a[10] ={9,34,24,56,31,24,66,3,45,98};
   cout<< std::inner_product(a,a+2,a+1,0,[](int x,int y){return x+y;},multiplies<int>())<<endl;
// 输出   1122     // 0 + 9*34 + 34*24 

}

3.atoi

   字符串 char*转  数字  ;

 cout<<std::atoi("13")+2<<endl;

4.itoa

  

itoa (int, char*, int)

整数 转 字符串

参数分别是   需要转化的整数    、接受返回值的字符串 , 转化的进制数

例如:

itoa(1130,s,16);
//输出  46a    //1130 的16进制形式  

5 is_sorted      

判断线性表是否按照规定顺序排好序

template<typename _ForwardIterator, typename _Compare>
  inline bool
  is_sorted(_ForwardIterator __first, _ForwardIterator __last,
     _Compare __comp)

其中 __comp可以是一个二元的自定义函数,用来比较前后两个数的是否按照要求比较;默认是从小到大

6  sort 

template<typename _RandomAccessIterator, typename _Compare>
  inline void
  sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
_Compare __comp)

例如

#include<algorithm>
#include<iostream>
using namespace std;
print(int a[],int len); //打印数组
int main(){
  int a[10] ={9,34,24,56,31,24,66,3,45,98};
    std::sort(a,a+9,[](int x,int y){return y<x;}); //对前面9个数从大到小排序
    print(a,10);
    cout<<std::is_sorted(a,a+9,[](int x,int y){return y<x;})<<endl;
    //判断前面9个数是否从大到小排序
}

输出:

66  56  45  34  31  24  24  9  3  98

1

7. fill

template<typename _ForwardIterator, typename _Tp>
  inline void
  fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)

从线性表  __first 到 __last  的值全部用__value 替换

8 mismatch

template<typename _InputIterator1, typename _InputIterator2,
  typename _BinaryPredicate>
  pair<_InputIterator1, _InputIterator2>
  mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
    _InputIterator2 __first2, _BinaryPredicate __binary_pred)

返回第一个匹配不上的匹配对。其中 __binary_pred 是匹配条件,默认是相等。

#include<algorithm>
#include<iostream>
using namespace std;
int main(){
   int a[10] ={9,34,24,56,31,24,66,3,45,98};
   int b[10] ={9,34,24,56,71,24,66,2,45,98};
   pair<int *, int *> pai=mismatch(a, a + 10, b,[](int x,int y){return y>=x;});
   cout<<*(pai.first)<<" notMatch "<<*(pai.second)<<endl;

    //输出 : 3 notMatch 2
}

猜你喜欢

转载自blog.csdn.net/superSmart_Dong/article/details/109437978