/*equal: 函数原型: template <class InputIterator1,class InputIterator2> bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2); template <class InputIterator1,class InputIterator2 class Predicate> bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2, Predicate pred); 功能: 第一个版本将序列[first1,last1)中元素依次和从first2指向的元素开始进行是否 相等的比较,而不考虑第二个序列的大小。若相等,则返回true。第二个版本运用 pred进行比较。 equal_range: 函数原型: template <class FwdIt,class T> pair<FwdIt,FwdIt> equal_range(FwdIt first, FwdIt last,const T& val); template <class FwdIt,class T,class Pred> pair<FwdIt,FwdIt> equal_range(FwdIt first, FwdIt last,const T& val,Pred pr); 功能:当由区间[first,last}中的迭代器指定的元素序列是按operator<排 序时,第一个模板函数有效地返回pair(lower_bound(first,last,val), upper_bound(first,last,val)).也就是说,该函数会在区间检测到最大 的那个子区间,在这个子区间内可以随处插入val而不会影响它的次序。 第二个模板函数的行为和第一个相同,不过它用pr(X,Y)替代了operator< (X,Y); lower_bound: 函数原型: template <class FwdIt,class T> FwdIt lower_bound(FwdIt first,FwdIt last,const T& val); template <class FwdIt,class T,class Pred> FedIt lower_bound(FwdIt first,FwdIt last,const T& val, Pred pr); 功能:lower_bound返回一个iterator,它指向在[first,last)标记中的有序序列中插 入val而不影响序列的次序的第一个位置,而这个位置标记一个大于等于val的 值。第二个模板行为和第一个相同,不过它用pr(x,y)替代了operator(x,y). upper_bound: 函数原型: template <class FwdIt,class T> FwdIt upper_bound(FwdIt first,FwdIt last,const T& val); template <class FwdIt,class T,class Pred> FedIt upper_bound(FwdIt first,FwdIt last,const T& val, Pred pr); 功能:upper_bound返回一个iterator,它指向在[first,last)标记中的有序序列中插 入val而不影响序列的次序的最后一个位置,而这个位置标记一个大于val的 值。第二个模板行为和第一个相同,不过它用pr(x,y)替代了operator(x,y). */ #include <iostream>#include <algorithm>#include <vector> using namespace std; int main(){ int ia[] = {1,3,6,9,12,25,36,54}; vector<int> vec1(ia,ia+6); vector<int> vec2(ia,ia+8); assert( equal(vec1.begin(),vec1.end(),vec2.begin()) == true ); assert( lower_bound(vec1.begin(),vec1.end(),4) == vec1.begin()+2 ); assert( upper_bound(vec1.begin(),vec1.end(),6) == vec1.begin()+3 ); cout << "Test Succeed!" << endl; system("pause"); return 0;}

评论