生活随笔
收集整理的這篇文章主要介紹了
STL 之adjacent_find, merge,inplace_merge
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
adjacent_find: 查找符合某一準(zhǔn)測的連續(xù)元素的首次出現(xiàn)
merge:合并有序列表,結(jié)果仍是一個(gè)有序列表
inplace_merge:合并有序連續(xù)序列
聲明:
#include?<algorithm>?? template<class?forwardItr>?? forwardItr?adjacent_find(forwardItr?first,?forwardItr?last);?? template<class?forwardItr,?class?binaryPredicate>?? forwardItr?adjacent_find(forwardItr?first,?forwardItr?last,?binaryPredicate?op);?? ?? template<class?inputItr1,class?inputItr2,class?outputItr,?class?binaryPredicate>?? outputItr?merge(inputItr1?first1,inputItr1?last1,?inputItr2?first2,?inputItr2?last2,outputItr?destFirst);?? template<class?inputItr1,class?inputItr2,class?outputItr>?? outputItr?merge(inputItr1?first1,inputItr1?last1,?inputItr2?first2,?inputItr2?last2,outputItr?destFirst,?binaryPredicate?op);?? ?? template<class?biDirectionalItr>?? void?inplace_merge(biDirectionalItr?first,?biDirectionalItr?middle,?biDirectionalItr?last);?? template<class?biDirectionalItr,?class?binaryPredicate>?? void?inplace_merge(biDirectionalItr?first,?biDirectionalItr?middle,?biDirectionalItr?last,?binaryPredicate?op);??
示例代碼:
#include?<iostream>?? #include?<list>?? ?? #include?<string>?? #include?<numeric>?? #include?<iterator>?? #include?<vector>?? #include?<functional>?? ?? #include?<algorithm>?? ?? using?namespace?std;?? ?? int?main()?{?? ????int?list1[10]?=?{1,3,5,7,9,0,2,4,6,8};?? ????int?list2[10]?=?{0,1,1,2,3,4,4,5,6,6};?? ????int?list3[5]?=?{0,2,4,6,8};?? ????int?list4[5]?=?{1,3,5,7,9};?? ?? ????list<int>?intList(list2,list2+10);?? ????list<int>::iterator?listItr;?? ?? ????vector<int>?vecList(list1,list1+10);?? ????vector<int>::iterator?vecItr;?? ?? ????ostream_iterator<int>?srceen(cout?,?"?");?? ????cout?<<?"intList:"?<<?endl;?? ????copy(intList.begin(),intList.end(),srceen);?? ????cout?<<?endl;?? ?? ?????? ????listItr?=?adjacent_find(intList.begin(),intList.end());?? ????if?(listItr?!=?intList.end())?? ????{?? ????????cout?<<?"ajacent?equal?elements?are?found"?<<?endl;?? ????????cout?<<?"value:"?<<?*listItr?<<?endl;?? ????}?else?{?? ????????cout?<<?"No?adjacent?equal?element"?<<?endl;?? ????}?? ?? ????intList.clear();?? ?????? ?????? ????merge(list3,list3?+?5,list4,list4?+?5,?back_inserter(intList));?? ????cout?<<?"merge?list3?and?list4"?<<?endl;?? ????copy(intList.begin(),intList.end(),srceen);?? ????cout?<<?endl;?? ?? ?????? ????vecItr?=?adjacent_find(vecList.begin(),vecList.end(),greater<int>());?? ????cout?<<?">\n"?<<??*vecItr?<<?endl;?? ????vecItr++;?? ????cout?<<?*vecItr?<<?endl;?? ????cout?<<?"vecList:"?<<?endl;?? ????copy(vecList.begin(),vecList.end(),srceen);?? ????cout?<<?endl;?? ?? ?????? ????inplace_merge(vecList.begin(),vecItr,vecList.end());?? ????cout?<<?"vecList:"?<<?endl;?? ????copy(vecList.begin(),vecList.end(),srceen);?? ????cout?<<?endl;?? ????return?0;?? }??
運(yùn)行結(jié)果:
intList:
0 1 1 2 3 4 4 5 6 6
ajacent equal elements are found
value:1
merge list3 and list4
0 1 2 3 4 5 6 7 8 9
>
9
0
vecList:
1 3 5 7 9 0 2 4 6 8
vecList:
0 1 2 3 4 5 6 7 8 9
總結(jié)
以上是生活随笔為你收集整理的STL 之adjacent_find, merge,inplace_merge的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。