include中常用函式的使用

2021-10-05 02:40:24 字數 2761 閱讀 8126

#include< algorithm >是c++的標準模版庫(stl)中最重要的標頭檔案之一,提供了大量基於迭代器的非成員模板函式。以下將介紹幾個常用的庫函式:

反轉排序指定範圍中的元素,reverse(a,b) 可以將陣列指標在[a,b)之間的元素或容器的迭代器在[a,ib)範圍內的元素進行反轉。

程式示例:

#include

#include

#include

using

namespace std;

intmain()

;reverse

(a,a+6)

;for

(int i=

0;i<

10;i++

) cout<

string str=

"abcdefgh"

;reverse

(str.

begin()

+2,str.

begin()

+6);

for(

int j=

0;jlength()

;j++

) cout<

return0;

}

執行結果:

fill() 可以把陣列或容器中的某一段區間賦為某個相同的值。

程式示例:

#include

#include

using

namespace std;

intmain()

cout<

return0;

}

執行結果:

排序函式,預設為遞增排序。

如果需要遞減排序,需要增加乙個比較函式:

bool

cmp(

int a,

int b)

sort

(a,a+n,cmp)

;

程式示例:

#include

#include

using

namespace std;

bool

cmp(

int a,

int b)

intmain()

; cout<<

"排序前陣列為:"

;for

(i=0

;i<

7;i++

) cout<

sort

(a,a+7)

; cout<<

"遞增排後前陣列為:"

;for

(i=0

;i<

7;i++

) cout<

sort

(a,a+

7,cmp)

; cout<<

"遞減排後前陣列為:"

;for

(i=0

;i<

7;i++

) cout<

return0;

}

返回給定範圍中的元素組成的下乙個按字典序的排列,即給出乙個序列在全排列中的下乙個序列。

程式示例:

#include

#include

using

namespace std;

intmain()

;dowhile

(next_permutation

(a,a+3)

);return0;

}

執行結果:

lower_bound 和 upper_bound()需要用在乙個有序陣列或容器中。

lower_bound(first,last,val) 用來尋找在陣列或容器的[first,last)範圍內第乙個值大於等於val元素的位置,如果是陣列,返回該位置的指標;若果是容器,返回該位置的迭代器

upper_bound(first,last,val) 用來尋找在陣列或容器的[first,last)範圍內第乙個值大於val元素的位置,如果是陣列,返回該位置的指標;若果是容器,返回該位置的迭代器

程式示例:

#include

#include

using

namespace std;

intmain()

;int

*lowerpos=

lower_bound

(a,a+10,

3);int

*upperpos=

upper_bound

(a,a+10,

3); cout<<

"lower_bound(a,a+10,3)="

<

cout<<

"upper_bound(a,a+10,3)="

<

return0;

}

執行結果:

include及其常用函式

include algorithm意為 演算法 是c 的標準模版庫 stl 中最重要的標頭檔案之一,提供了大量基於迭代器的非成員模版函式。accumlate iterator 對標誌的序列中的元素之和,加到乙個由 init 指定的初始值上。過載的版本不再做加法,而是傳進來的二元操作符被應用到元素上。...

中常用的函式 遷移學習中常用的核函式

在遷移學習中,經常要計算不同域之間的分布距離,常用的方法如最大均值差異 maximum mean discrepancy 其中,分別表示源領域和目標領域的資料。表示函式空間 上的乙個泛函,將資料從原歐氏空間對映到再生核希爾伯特空間 rkhs 由於這個空間對於函式來說是內積完備的,更容易找乙個對映使得...

tensorflow中常用的函式

正文 一,tensorflow中有一類在tensor的某一維度上求值的函式。如 求最大值 tf.reduce max input tensor,reduction indices none,keep dims false,name none 求平均值tf.reduce mean input tens...