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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

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

發(fā)布時(shí)間:2024/10/14 c/c++ 74 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ : STL常用算法: inner_product , sort ,itoa 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

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 中,用來(lái)記錄線性表 從 __first 到 __last? ?中出現(xiàn)? __value的次數(shù)?

#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??類(lèi)似與算術(shù)運(yùn)算符? ,也可以是一個(gè)兩個(gè)參數(shù)的自定義函數(shù)。

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

__last1: 代表線性表的結(jié)束位置

__first2: 代表另一個(gè)線性表的起始為位置

__init : 代表初始值

__binary_op1: 兩個(gè)相之間的算術(shù)符 ,默認(rèn)是? 加法

__binary_op2? : 兩個(gè)線性表元素的算術(shù)符? ?默認(rèn)是乘法

函數(shù)作用: 返回和? ? ?__init數(shù)據(jù)類(lèi)型相同的數(shù)? ?

? 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*轉(zhuǎn)? 數(shù)字? ;

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

4.itoa

??

itoa (int, char*, int)

整數(shù) 轉(zhuǎn) 字符串

參數(shù)分別是? ?需要轉(zhuǎn)化的整數(shù)? ? 、接受返回值的字符串 , 轉(zhuǎn)化的進(jìn)制數(shù)

例如:

itoa(1130,s,16); //輸出 46a //1130 的16進(jìn)制形式

?

5 is_sorted? ? ??

判斷線性表是否按照規(guī)定順序排好序

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

其中?__comp可以是一個(gè)二元的自定義函數(shù),用來(lái)比較前后兩個(gè)數(shù)的是否按照要求比較;默認(rèn)是從小到大

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); //打印數(shù)組 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;}); //對(duì)前面9個(gè)數(shù)從大到小排序print(a,10);cout<<std::is_sorted(a,a+9,[](int x,int y){return y<x;})<<endl;//判斷前面9個(gè)數(shù)是否從大到小排序 }

輸出:

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)

返回第一個(gè)匹配不上的匹配對(duì)。其中?__binary_pred 是匹配條件,默認(rèn)是相等。

?

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

?

總結(jié)

以上是生活随笔為你收集整理的C++ : STL常用算法: inner_product , sort ,itoa的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。