C ++:STLで一般的に使用されるアルゴリズム:inner_product、sort、itoa

目次

1.std :: count

2.std :: inner_product

3.atoi

4.itoa

5 is_sorted      

6ソート 

7.塗りつぶし

8ミスマッチ


1.std :: count

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

ヘッダーファイルアルゴリズムでは、__ 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は算術演算子に似ており、2つのパラメーターを持つカスタム関数にすることもできます。

__first1:線形テーブルの開始位置を表します

__last1:線形テーブルの終了位置を表します

__first2:別の線形テーブルの開始位置を表します

__init:初期値を表します

__binary_op1:2つのフェーズ間の算術演算、デフォルトは加算

__binary_op2:2つの線形テーブル要素の算術デフォルトは乗算です

関数の役割:__ 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)

整数から文字列

パラメータは、変換する必要のある整数、戻り値を受け入れる文字列、および変換された16進数です。

例えば:

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は、要件に従って前後の2つの数値を比較するかどうかを比較するバイナリカスタム関数にすることができます。デフォルトは小さいものから大きいものまでです。

6ソート 

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.塗りつぶし

template <typename _ForwardIterator、typename _Tp>
  inline void
  fill(_ForwardIterator __first、_ForwardIterator __last、const _Tp&__ value)

線形テーブル__firstから__lastまでの値はすべて__valueに置き換えられます

 

8ミスマッチ

template <typename _InputIterator1、typename _InputIterator2、
  typename _BinaryPredicate> 
  pair <_InputIterator1、_InputIterator2>
  不一致(_InputIterator1 __first1、_InputIterator1 __last1、
    _InputIterator2 __first2、_BinaryPredicate __binary_d

一致しない最初の一致ペアを返します。ここで、__ 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