仪征市城乡建设局网站,wordpress插件制作,用自己电脑做网站,怎样在手机上制作网站Equal
两个序列在[first,last)区间内相等#xff0c;equal()返回true。以第一个序列作为基准#xff0c;如果第二序列元素多不予考虑#xff0c;如果要保证两个序列完全相等需要比较元素的个数
if(vec1.size() vec2.size() equal(vec1.begin(),vec1.end(),vec2…Equal
两个序列在[first,last)区间内相等equal()返回true。以第一个序列作为基准如果第二序列元素多不予考虑如果要保证两个序列完全相等需要比较元素的个数
if(vec1.size() vec2.size() equal(vec1.begin(),vec1.end(),vec2.begin()));
或者可以使用容器提供的equality操作符例如 vec1 vec2如果第二序列比第一序列的元素少 算法内部进行迭代行为的时候会超越序列的尾端造成不可预测的结果第一版本缺省使用元素型别所提供的equality 操作符来进行大小的比较第二版本使用指定的仿函数pred作为比较的依据 template class InputIterator1,class InputIterator2
inline bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2){//将序列一走过一遍序列二亦步亦趋//如果序列一的元素多过序列二的元素个数 就会糟糕了while(first1 ! first2){if(!(*first1 *first2)){return false;}first1;first2;}//第二个版本
// for (; first1 ! last1;first1,first2){
// if(*first1 ! *first2){
// return false;
// }
// }return true;
}//使用自定义的 仿函数pred作为比较的依据
template class InputIterator1,class InputIterator2,class BinaryOperation
inline bool equal(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,BinaryOperation binary_pred){while(first1 ! first2){if(!binary_pred(*first1,*first2)){return false;}first1;first2;}return true;
}
#include iostream //std::cout
#include algorithm //std::equal
#include vector //std::vectorbool my_predicate(int i,int j){return (ij);
}int main(){int myints[] {20,40,60,80,100};std::vectorintmy_vector(myints,myints5);//using default comparison:if(std::equal(my_vector.begin(),my_vector.end(),myints)){std::cout equal! std::endl;}else{std::cout differ! std::endl;}my_vector[3] 94;if(std::equal(my_vector.begin(),my_vector.end(),myints, my_predicate)){std::cout equal! std::endl;}else{std::cout differ! std::endl;}return 0;
}
fill
将指定[first,last)内的所有元素使用新值填充
templateclass ForwardIterator,class T
void fill(ForwardIterator first,ForwardIterator last,const T value){while (first!last){*first value; //设定新值first; //迭代走过整个区间}
}
#include iostream //std::cout
#include algorithm //std::fill
#include vector //std::vectorbool my_predicate(int i,int j){return (ij);
}int main(){std::vectorintmy_vector(8); //0 0 0 0 0 0 0 0std::fill(my_vector.begin(),my_vector.begin()4,5);std::fill(my_vector.begin()3,my_vector.end()-2,8);std::cout my_vector contains: std::endl;for(std::vectorint::iterator it my_vector.begin();it ! my_vector.end();it){std::cout *it ;}return 0;
} fill_n
将本地[first,last)内的前n个元素改写为新的数值返回迭代器指向被填入的最后一个元素的下一个位置
templateclass OutputIterator,class Size,class T
OutputIterator fill_n(OutputIterator first,Size n,const Tvalue){while (n0){*first value;--n;first;}return first;
}
如果n超过了容器的容量的上限会造成什么样的后果呢 int ia[3] {0,1,2};std::vectorintiv(ia,ia3);std::fill_n(iv.begin(),5,7);
迭代器进行的是assignment操作是一种覆写操作一旦超越了容器的大小会造成不可预期的结果。解决方法使用insert产生一个具有插入性质而不是覆写能力的迭代器。inserter()可以产生一个用来修饰迭代器的配接器用法如下 int ia[3] {0,1,2};std::vectorintiv(ia,ia3);
// std::fill_n(iv.begin(),5,7); //7 7 7std::fill_n(std::inserter(iv,iv.begin()),5,7); // 7 7 7 7 7 0 1 2
iter_swap template class ForwardIterator1, class ForwardIterator2void iter_swap (ForwardIterator1 a, ForwardIterator2 b)
{swap (*a, *b);
}
// iter_swap example
#include iostream // std::cout
#include algorithm // std::iter_swap
#include vector // std::vectorint main () {int myints[]{10,20,30,40,50 }; // myints: 10 20 30 40 50std::vectorint myvector (4,99); // myvector: 99 99 99 99std::iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50// myvector: [10] 99 99 99std::iter_swap(myints3,myvector.begin()2); // myints: 99 20 30 [99] 50// myvector: 10 99 [40] 99std::cout myvector contains:;for (std::vectorint::iterator itmyvector.begin(); it!myvector.end(); it)std::cout *it;std::cout \n;return 0;
} lexicographical_compare
操作指针比较[first1 , last1) 和 [first2 , last2)对应位置上的元素大小的比较持续到(1)某一组元素数据不相等(2)二者完全相等同时到达两个队列的末尾(3)到达last1或者last2要求第一序列按照字典排序方式而言不小于第二序列lexicographical_compare - C Reference
templateclass InputIterator1,class InputIterator2
bool lexicographical_compare(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,InputIterator2 last2){while (first1!last1){if (first2 last2 || *first2 *first1){return false;} else if(*first1 *first2){return true;}first1;first2;}return (first2 ! last2);
}// lexicographical_compare example
#include iostream // std::cout, std::boolalpha
#include algorithm // std::lexicographical_compare
#include cctype // std::tolower// a case-insensitive comparison function:
bool mycomp (char c1, char c2)
{ return std::tolower(c1)std::tolower(c2); }int main () {char foo[]Apple;char bar[]apartment;std::cout std::boolalpha;std::cout Comparing foo and bar lexicographically (foobar):\n;std::cout Using default comparison (operator): ;std::cout std::lexicographical_compare(foo,foo5,bar,bar9);std::cout \n;std::cout Using mycomp as comparison object: ;std::cout std::lexicographical_compare(foo,foo5,bar,bar9,mycomp);std::cout \n;return 0;
} max
取两个对象中的最大数值版本一使用greater-than操作符号进行大小的比较版本二使用自定义comp函数比较
template class T
const Tmax(const Ta,const Tb){return (ab)?a:b;
}template class T,class BinaryOperation
const Tmax(const Ta,const Tb,BinaryOperation binary_pred){return (binary_pred(a,b))?a:b;
}
min
取两个对象中的最小数值版本一使用less-than操作符号进行大小的比较版本二使用自定义comp函数比较
template class T
const Tmin(const Ta,const Tb){return (ab)?a:b;
}template class T,class BinaryOperation
const Tmin(const Ta,const Tb,BinaryOperation binary_pred){return (binary_pred(a,b))?a:b;
}
mismatch
判断两个区间的第一个不匹配的点返回一个由两个迭代器组成的pairpair的第一个迭代器指向的是第一区间第一个不匹配的点第二个迭代器指向第二个区间的不匹配的点需要首先检查迭代器是否不等于容器的end() 如果两个序列的所有元素对应都匹配返回的是两个序列各自的last迭代器缺省情况下使用equality操作符号来比较元素第二版本允许用户指定自己的比较操作需要第二个序列的元素个数要大于第一个序列否则会发生不可预期的行为template class InputIterator1,class InputIterator2
std::pairInputIterator1,InputIterator2mismatch(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2){while ((first1!last1)(*first1 *first2)){first1;first2;}return std::make_pair(first1,first2);
}
第二版本 pred(*first1,*first2)
swap
对调元素的内容
template class T
inline void swap(Ta,Tb){T tmp (a);a b;b tmp;
}