C++: STL commonly used algorithms: inner_product, sort, itoa

table of Contents

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)

In the header file algorithm, it is used to record the number of occurrences of __value in the linear table from __first to __last 

#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)

Among them, __binary_op1 and __binary_op2 are similar to arithmetic operators and can also be a custom function with two parameters.

__first1: Represents the starting position of the linear table

__last1: Represents the end position of the linear table

__first2: represents the starting position of another linear table

__init: represents the initial value

__binary_op1: Arithmetic between two phases, the default is addition

__binary_op2: Arithmetic for two linear table elements. The default is multiplication

Function role: return a number with the same data type as __init   

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

E.g

#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

   String char* to number;

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

4.itoa

  

itoa (int, char*, int)

Integer to string

The parameters are the integer that needs to be converted, the string that accepts the return value, and the converted hexadecimal number

E.g:

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

 

5 is_sorted      

Determine whether the linear table is sorted according to the specified order

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

Among them, __comp can be a binary custom function to compare whether the two numbers before and after are compared according to the requirements; the default is from small to large

6  sort 

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

E.g

#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个数是否从大到小排序
}

Output:

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)

The values ​​from the linear table __first to __last are all replaced with __value

 

8 mismatch

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

Returns the first matching pair that does not match. Where __binary_pred is the matching condition, and the default is equal.

 

#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
}

 

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/109437978