生活随笔
收集整理的這篇文章主要介紹了
STL中算法锦集(一)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
STL中算法錦集(一)
文章目錄 STL中算法錦集(一) 一、< algorithm > 1.std::adjacent_find 2.std::all_of 3.std::any_of 4.std::binary_search 5.std::copy 6.std::copy_backward 7.std::copy_if 8.std::copy_n 9.std::count 10.std::count_if
一、< algorithm >
雖然這一部分不一定要全部掌握,但是掌握可以更快捷、方便的編程
1.std::adjacent_find
template < class ForwardIterator > ForwardIterator adjacent_find
( ForwardIterator first
, ForwardIterator last
) ; template < class ForwardIterator , class BinaryPredicate > ForwardIterator adjacent_find
( ForwardIterator first
, ForwardIterator last
, BinaryPredicate pred
) ;
ForwardIterator first:區(qū)間的開(kāi)始 ForwardIterator last:區(qū)間的結(jié)束 BinaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
作用:在[first,last)這個(gè)范圍中搜索兩個(gè)連續(xù)、滿(mǎn)足pred條件、且是第一次出現(xiàn)的元素位置 。 返回值:如果找到這兩個(gè)元素對(duì),返回first迭代器,如果沒(méi)有找到這兩個(gè)元素對(duì),則返回last迭代器 實(shí)現(xiàn):
template < class ForwardIterator >
ForwardIterator adjacent_find
( ForwardIterator first
, ForwardIterator last
)
{ if ( first
!= last
) { ForwardIterator next
= first
; ++ next
; while ( next
!= last
) { if ( * first
== * next
) return first
; ++ first
; ++ next
; } } return last
;
}
#include <iostream>
#include <algorithm>
#include <vector> bool myfunction
( int i
, int j
)
{ return ( i
== j
) ;
} int main
( )
{ int myints
[ ] = { 5 , 20 , 5 , 30 , 30 , 20 , 10 , 10 , 20 } ; std
:: vector
< int > myvector
( myints
, myints
+ 8 ) ; std
:: vector
< int > :: iterator it
; it
= std
:: adjacent_find
( myvector
. begin ( ) , myvector
. end ( ) ) ; if ( it
!= myvector
. end ( ) ) std
:: cout
<< "the first pair of repeated elements are: " << * it
<< '\n' ; it
= std
:: adjacent_find
( ++ it
, myvector
. end ( ) , myfunction
) ; if ( it
!= myvector
. end ( ) ) std
:: cout
<< "the second pair of repeated elements are: " << * it
<< '\n' ; return 0 ;
} Output
:
the first pair of repeated elements are
: 30
the second pair of repeated elements are
: 10
2.std::all_of
template < class InputIterator , class UnaryPredicate > bool all_of
( InputIterator first
, InputIterator last
, UnaryPredicate pred
) ;
InputIterator first:區(qū)間的開(kāi)始 InputIterator last:區(qū)間的結(jié)束 UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
作用:判斷[first,last)區(qū)間中的所有元素是否滿(mǎn)足pred條件 返回值:如果全部滿(mǎn)足pred條件返回true,反之有一個(gè)不滿(mǎn)足條件返回false 實(shí)現(xiàn):
template < class InputIterator , class UnaryPredicate > bool all_of
( InputIterator first
, InputIterator last
, UnaryPredicate pred
)
{ while ( first
!= last
) { if ( ! pred ( * first
) ) return false ; ++ first
; } return true ;
}
#include <iostream>
#include <algorithm>
#include <array> int main
( ) { std
:: array
< int , 8 > foo
= { 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 } ; if ( std
:: all_of ( foo
. begin ( ) , foo
. end ( ) , [ ] ( int i
) { return i
% 2 ; } ) ) std
:: cout
<< "All the elements are odd numbers.\n" ; return 0 ;
} Output
:
All the elements are odd numbers
.
3.std::any_of
template < class InputIterator , class UnaryPredicate > bool any_of
( InputIterator first
, InputIterator last
, UnaryPredicate pred
) ;
InputIterator first:區(qū)間的開(kāi)始 InputIterator last:區(qū)間的結(jié)束 UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
作用:判斷[first,last)區(qū)間中是否有元素是否滿(mǎn)足pred條件 返回值:如果有任意一個(gè)元素滿(mǎn)足pred條件就返回true,反之返回false 實(shí)現(xiàn):
template < class InputIterator , class UnaryPredicate > bool any_of
( InputIterator first
, InputIterator last
, UnaryPredicate pred
)
{ while ( first
!= last
) { if ( pred ( * first
) ) return true ; ++ first
; } return false ;
}
#include <iostream>
#include <algorithm>
#include <array> int main
( ) { std
:: array
< int , 7 > foo
= { 0 , 1 , - 1 , 3 , - 3 , 5 , - 5 } ; if ( std
:: any_of ( foo
. begin ( ) , foo
. end ( ) , [ ] ( int i
) { return i
< 0 ; } ) ) std
:: cout
<< "There are negative elements in the range.\n" ; return 0 ;
} Output
:
There are negative elements in the range
.
4.std::binary_search
template < class ForwardIterator , class T > bool binary_search
( ForwardIterator first
, ForwardIterator last
, const T
& val
) ; template < class ForwardIterator , class T , class Compare > bool binary_search
( ForwardIterator first
, ForwardIterator last
, const T
& val
, Compare comp
) ;
ForwardIterator first:區(qū)間的開(kāi)始 ForwardIterator last:區(qū)間的結(jié)束 const T& val:待搜索的val Compare comp:自定義的比較器,因?yàn)樵乜赡苁亲远x的
作用:二分查找區(qū)間中是否包含val元素 返回值:如果包含返回true,反之返回false 實(shí)現(xiàn):
template < class ForwardIterator , class T > bool binary_search
( ForwardIterator first
, ForwardIterator last
, const T
& val
)
{ first
= std
:: lower_bound ( first
, last
, val
) ; return ( first
!= last
&& ! ( val
< * first
) ) ;
}
#include <iostream>
#include <algorithm>
#include <vector> bool myfunction
( int i
, int j
) { return ( i
< j
) ; } int main
( ) { int myints
[ ] = { 1 , 2 , 3 , 4 , 5 , 4 , 3 , 2 , 1 } ; std
:: vector
< int > v ( myints
, myints
+ 9 ) ; std
:: sort
( v
. begin ( ) , v
. end ( ) ) ; std
:: cout
<< "looking for a 3... " ; if ( std
:: binary_search
( v
. begin ( ) , v
. end ( ) , 3 ) ) std
:: cout
<< "found!\n" ; else std
:: cout
<< "not found.\n" ; std
:: sort
( v
. begin ( ) , v
. end ( ) , myfunction
) ; std
:: cout
<< "looking for a 6... " ; if ( std
:: binary_search
( v
. begin ( ) , v
. end ( ) , 6 , myfunction
) ) std
:: cout
<< "found!\n" ; else std
:: cout
<< "not found.\n" ; return 0 ;
} Output
:
looking
for a
3. . . found
!
looking
for a
6. . . not found
.
5.std::copy
template < class InputIterator , class OutputIterator > OutputIterator copy
( InputIterator first
, InputIterator last
, OutputIterator result
) ;
InputIterator first:區(qū)間的開(kāi)始 InputIterator last:區(qū)間的結(jié)束 OutputIterator result:拷貝區(qū)間的開(kāi)始位置 注意[first,last)區(qū)間和result不能夠重疊,否則會(huì)報(bào)錯(cuò)
作用:把[first,last)區(qū)間當(dāng)中的所有元素拷貝到result空間中 返回值:返回拷貝后的空間末尾位置的迭代器 實(shí)現(xiàn):
template < class InputIterator , class OutputIterator > OutputIterator copy
( InputIterator first
, InputIterator last
, OutputIterator result
)
{ while ( first
!= last
) { * result
= * first
; ++ result
; ++ first
; } return result
;
}
#include <iostream>
#include <algorithm>
#include <vector> int main
( ) { int myints
[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 } ; std
:: vector
< int > myvector
( 7 ) ; std
:: copy
( myints
, myints
+ 7 , myvector
. begin ( ) ) ; std
:: cout
<< "myvector contains:" ; for ( std
:: vector
< int > :: iterator it
= myvector
. begin ( ) ; it
!= myvector
. end ( ) ; ++ it
) std
:: cout
<< ' ' << * it
; std
:: cout
<< '\n' ; return 0 ;
} Output
:
myvector contains
: 10 20 30 40 50 60 70
6.std::copy_backward
template < class BidirectionalIterator1 , class BidirectionalIterator2 > BidirectionalIterator2 copy_backward
( BidirectionalIterator1 first
, BidirectionalIterator1 last
, BidirectionalIterator2 result
) ;
BidirectionalIterator1 first:區(qū)間的開(kāi)始 BidirectionalIterator1 last:區(qū)間的結(jié)束 BidirectionalIterator2 result:拷貝區(qū)間的末尾位置 注意[first,last)區(qū)間和result能夠不會(huì)重疊,會(huì)報(bào)錯(cuò) copy是正向copy的,而copy_backward是反向copy的,copy_backward區(qū)間如果沖突就會(huì)把前面區(qū)間的元素覆蓋掉
作用:把[first,last)區(qū)間當(dāng)中的所有元素拷貝到result空間中 返回值:返回拷貝后的空間第一個(gè)位置的迭代器 實(shí)現(xiàn):
template < class BidirectionalIterator1 , class BidirectionalIterator2 > BidirectionalIterator2 copy_backward
( BidirectionalIterator1 first
, BidirectionalIterator1 last
, BidirectionalIterator2 result
)
{ while ( last
!= first
) * ( -- result
) = * ( -- last
) ; return result
;
}
#include <iostream>
#include <algorithm>
#include <vector> int main
( ) { std
:: vector
< int > myvector
; for ( int i
= 1 ; i
<= 5 ; i
++ ) myvector
. push_back ( i
* 10 ) ; myvector
. resize ( myvector
. size ( ) + 3 ) ; std
:: copy_backward
( myvector
. begin ( ) , myvector
. begin ( ) + 5 , myvector
. end ( ) ) ; std
:: cout
<< "myvector contains:" ; for ( std
:: vector
< int > :: iterator it
= myvector
. begin ( ) ; it
!= myvector
. end ( ) ; ++ it
) std
:: cout
<< ' ' << * it
; std
:: cout
<< '\n' ; return 0 ;
} Output
:
myvector contains
: 10 20 30 10 20 30 40 50
7.std::copy_if
template < class InputIterator , class OutputIterator , class UnaryPredicate > OutputIterator copy_if
( InputIterator first
, InputIterator last
, OutputIterator result
, UnaryPredicate pred
) ;
InputIterator first:區(qū)間的開(kāi)始 InputIterator last:區(qū)間的結(jié)束 OutputIterator result:拷貝區(qū)間的開(kāi)始位置 UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
作用:[first,last)區(qū)間中的元素如果滿(mǎn)足pred條件就copy到result中 返回值:返回拷貝后的空間末尾位置的迭代器 實(shí)現(xiàn):
template < class InputIterator , class OutputIterator , class UnaryPredicate > OutputIterator copy_if
( InputIterator first
, InputIterator last
, OutputIterator result
, UnaryPredicate pred
)
{ while ( first
!= last
) { if ( pred ( * first
) ) { * result
= * first
; ++ result
; } ++ first
; } return result
;
}
#include <iostream>
#include <algorithm>
#include <vector> int main
( ) { std
:: vector
< int > foo
= { 25 , 15 , 5 , - 5 , - 15 } ; std
:: vector
< int > bar
( foo
. size ( ) ) ; auto it
= std
:: copy_if
( foo
. begin ( ) , foo
. end ( ) , bar
. begin ( ) , [ ] ( int i
) { return ! ( i
< 0 ) ; } ) ; bar
. resize ( std
:: distance ( bar
. begin ( ) , it
) ) ; std
:: cout
<< "bar contains:" ; for ( int & x
: bar
) std
:: cout
<< ' ' << x
; std
:: cout
<< '\n' ; return 0 ;
} Output
:
bar contains
: 25 15 5
8.std::copy_n
template < class InputIterator , class Size , class OutputIterator > OutputIterator copy_n
( InputIterator first
, Size n
, OutputIterator result
) ;
InputIterator first:區(qū)間的開(kāi)始 Size n:拷貝元素的個(gè)數(shù) OutputIterator result:拷貝區(qū)間的開(kāi)始位置
作用:從first拷貝n個(gè)元素到result當(dāng)中 返回值:返回拷貝區(qū)間的末尾位置的迭代器 實(shí)現(xiàn):
template < class InputIterator , class Size , class OutputIterator > OutputIterator copy_n
( InputIterator first
, Size n
, OutputIterator result
)
{ while ( n
> 0 ) { * result
= * first
; ++ result
; ++ first
; -- n
; } return result
;
}
#include <iostream>
#include <algorithm>
#include <vector> int main
( ) { int myints
[ ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 } ; std
:: vector
< int > myvector
; myvector
. resize ( 7 ) ; std
:: copy_n
( myints
, 7 , myvector
. begin ( ) ) ; std
:: cout
<< "myvector contains:" ; for ( std
:: vector
< int > :: iterator it
= myvector
. begin ( ) ; it
!= myvector
. end ( ) ; ++ it
) std
:: cout
<< ' ' << * it
; std
:: cout
<< '\n' ; return 0 ;
} Output
:
myvector contains
: 10 20 30 40 50 60 70
9.std::count
template < class InputIterator , class T > typename iterator_traits
< InputIterator
> :: difference_typecount
( InputIterator first
, InputIterator last
, const T
& val
) ;
InputIterator first:區(qū)間的開(kāi)始 InputIterator last:區(qū)間的結(jié)束 const T& val:待計(jì)數(shù)的val
作用:[first,last)區(qū)間中值為val的元素的個(gè)數(shù) 返回值:返回值為val的元素的個(gè)數(shù) 實(shí)現(xiàn):
template < class InputIterator , class T > typename iterator_traits
< InputIterator
> :: difference_typecount
( InputIterator first
, InputIterator last
, const T
& val
)
{ typename iterator_traits
< InputIterator
> :: difference_type ret
= 0 ; while ( first
!= last
) { if ( * first
== val
) ++ ret
; ++ first
; } return ret
;
}
#include <iostream>
#include <algorithm>
#include <vector> int main
( ) { int myints
[ ] = { 10 , 20 , 30 , 30 , 20 , 10 , 10 , 20 } ; int mycount
= std
:: count
( myints
, myints
+ 8 , 10 ) ; std
:: cout
<< "10 appears " << mycount
<< " times.\n" ; std
:: vector
< int > myvector
( myints
, myints
+ 8 ) ; mycount
= std
:: count
( myvector
. begin ( ) , myvector
. end ( ) , 20 ) ; std
:: cout
<< "20 appears " << mycount
<< " times.\n" ; return 0 ;
} Output
:
10 appears
3 times
.
20 appears
3 times
.
10.std::count_if
template < class InputIterator , class UnaryPredicate > typename iterator_traits
< InputIterator
> :: difference_typecount_if
( InputIterator first
, InputIterator last
, UnaryPredicate pred
) ;
InputIterator first:區(qū)間的開(kāi)始 InputIterator last:區(qū)間的結(jié)束 UnaryPredicate pred:自定義比較規(guī)則,可以是函數(shù)指針或者仿函數(shù)對(duì)象、lambda表達(dá)式
作用:求[first,last)區(qū)間內(nèi)滿(mǎn)足pred條件的元素的個(gè)數(shù) 返回值:滿(mǎn)足pred條件的元素的個(gè)數(shù) 實(shí)現(xiàn):
template < class InputIterator , class UnaryPredicate > typename iterator_traits
< InputIterator
> :: difference_typecount_if
( InputIterator first
, InputIterator last
, UnaryPredicate pred
)
{ typename iterator_traits
< InputIterator
> :: difference_type ret
= 0 ; while ( first
!= last
) { if ( pred ( * first
) ) ++ ret
; ++ first
; } return ret
;
}
#include <iostream>
#include <algorithm>
#include <vector> bool IsOdd
( int i
) { return ( ( i
% 2 ) == 1 ) ; } int main
( ) { std
:: vector
< int > myvector
; for ( int i
= 1 ; i
< 10 ; i
++ ) myvector
. push_back ( i
) ; int mycount
= count_if
( myvector
. begin ( ) , myvector
. end ( ) , IsOdd
) ; std
:: cout
<< "myvector contains " << mycount
<< " odd values.\n" ; return 0 ;
} Output
:
myvector contains
5 odd values
.
總結(jié)
以上是生活随笔 為你收集整理的STL中算法锦集(一) 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。