返回
adjacent_find: 查找符合某一准测的连续元素的首次出现
merge:合并有序列表,结果仍是一个有序列表
inplace_merge:合并有序连续序列
声明:
#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; // adjacent_find 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 inplace_merge(vecList.begin(),vecItr,vecList.end()); cout << "vecList:" << endl; copy(vecList.begin(),vecList.end(),srceen); cout << endl; return 0; }
运行结果:
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