日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

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

發布時間:2024/10/14 c/c++ 80 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。