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 _Tpinner_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 boolis_sorted(_ForwardIterator __first, _ForwardIterator __last,_Compare __comp)其中?__comp可以是一個二元的自定義函數,用來比較前后兩個數的是否按照要求比較;默認是從小到大
6? sort?
template<typename _RandomAccessIterator, typename _Compare>inline voidsort(_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 }?
總結
以上是生活随笔為你收集整理的C++ : STL常用算法: inner_product , sort ,itoa的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ : 返回两个字符串的最长公共字符
- 下一篇: C++ : 二进制法生成子集