1. 简介
函数 |
作用 |
文档 |
equal(beg1,end1,beg2) |
判断[beg1 ,end1 )与[beg2 ,end2 )内元素都相等 |
equal() |
equal(beg1,end1,beg2,pred) |
使用pred 函数代替默认的== 操作符。 |
equal() |
includes(beg1,end1,beg2,end2) |
判断[beg1 ,end1 )是否包含[beg2 ,end2 ),使用底层元素的< 操作符,成功返回true。重载版本使用用户输入的函数。 |
includes() |
includes(beg1,end1,beg2,end2,comp) |
将函数comp 代替< 操作符,执行includes() 。 |
includes() |
lexicographical_compare(beg1,end1,beg2,end2) |
按字典序判断[beg1 ,end1 )是否小于[beg2 ,end2 ) |
lexicographical_compare() |
lexicographical_compare(beg1,end1,beg2,end2,comp) |
将函数comp 代替< 操作符,执行lexicographical_compare() 。 |
lexicographical_compare() |
mismatch(beg1,end1,beg2) |
并行比较[beg1 ,end1 )与[beg2 ,end2 ),指出第一个不匹配的位置,返回一对iterator ,标志第一个不匹配元素位置。如果都匹配,返回每个容器的end 。 |
mismatch() |
mismatch(beg1,end1,beg2,pred) |
使用pred 函数代替默认的== 操作符。 |
mismatch() |
2. 示例代码
// equal algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::equal
#include <vector> // std::vector
bool mypredicate (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100
std::vector<int>myvector (myints,myints+5); // myvector: 20 40 60 80 100
// using default comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
myvector[3]=81; // myvector: 20 40 60 81 100
// using predicate comparison:
if ( std::equal (myvector.begin(), myvector.end(), myints, mypredicate) )
std::cout << "The contents of both sequences are equal.\n";
else
std::cout << "The contents of both sequences differ.\n";
return 0;
}
// includes algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::includes, std::sort
bool myfunction (int i, int j) { return i<j; }
int main () {
int container[] = {5,10,15,20,25,30,35,40,45,50};
int continent[] = {40,30,20,10};
std::sort (container,container+10);
std::sort (continent,continent+4);
// using default comparison:
if ( std::includes(container,container+10,continent,continent+4) )
std::cout << "container includes continent!\n";
// using myfunction as comp:
if ( std::includes(container,container+10,continent,continent+4, myfunction) )
std::cout << "container includes continent!\n";
return 0;
}
// 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 (foo<bar):\n";
std::cout << "Using default comparison (operator<): ";
std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9);
std::cout << '\n';
std::cout << "Using mycomp as comparison object: ";
std::cout << std::lexicographical_compare(foo,foo+5,bar,bar+9,mycomp);
std::cout << '\n';
return 0;
}
// mismatch algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::mismatch
#include <vector> // std::vector
#include <utility> // std::pair
bool mypredicate (int i, int j) {
return (i==j);
}
int main () {
std::vector<int> myvector;
for (int i=1; i<6; i++) myvector.push_back (i*10); // myvector: 10 20 30 40 50
int myints[] = {10,20,80,320,1024}; // myints: 10 20 80 320 1024
std::pair<std::vector<int>::iterator,int*> mypair;
// using default comparison:
mypair = std::mismatch (myvector.begin(), myvector.end(), myints);
std::cout << "First mismatching elements: " << *mypair.first;
std::cout << " and " << *mypair.second << '\n';
++mypair.first; ++mypair.second;
// using predicate comparison:
mypair = std::mismatch (mypair.first, myvector.end(), mypair.second, mypredicate);
std::cout << "Second mismatching elements: " << *mypair.first;
std::cout << " and " << *mypair.second << '\n';
return 0;
}
3. 练习