主要函数 //equal template<class InIt1, class InIt2> bool equal(InIt1 first, InIt1 last, InIt2 x); template<class InIt1, class InIt2, class Pred> bool equal(InIt1 first, InIt2 last, InIt2 x, class Pred pr) //mismatch template<class InIt1, class InIt2> pair<InIt1, InIt2>mismatch(InIt1 first, InIt1 last, InIt2 x); template<class InIt1, class InIt2, class Pred> pair<InIt1,InIt2>mismatch(InIt1 first, InIt1 last, InIt2 x, Pred pr) 比较两个整型数组是否相等,以及不相等的首对。 #include <iostream> #include <algorithm> #include <cstdlib> using namespace std; void show(int a[], int size) { for(int i = 0; i < size; i++) { cout << a[i] << " "; } cout << endl; } int main(int argc, char argv[]) { int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int b[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int c[] = {1, 3, 2, 4, 5, 6, 7, 8, 9}; const int nSize = sizeof(a)/sizeof(int); cout << "a[] = "; show(a, nSize); cout << "b[] = "; show(b, nSize); cout << "c[] = "; show(c, nSize); cout << "Result of comparision(a,b): " << boolalpha << equal(a, a+nSize, b) << endl; pair<int *, int *> result = mismatch(a, a+nSize, c); cout << "Result of comparsion(a,c): " << *(result.first) << " <-> " << *(result.second) << endl; system("pause"); return 0; } mismatch值student对象 #include <iostream> #include <vector> #include <algorithm> #include <cstdlib> #include <string> using namespace std; class Student { private: string stuName; int stuNumber; int stuGrade; public: Student(const int number = 0, const string name = "none", const int grade = 0); const int number()const { return stuNumber; } const string name() const { return stuName; } const int grade() const { return stuGrade; } bool operator ==( const Student &stu) const { return stuGrade == stu.stuGrade; } }; Student::Student(const int number, const string name, const int grade) { stuNumber = number; stuName = name; stuGrade = grade; } int main(int argc, char *argv[]) { Student stu1(10001, "张三",70); Student stu2(10002, "李四",70); Student stu3(10003, "王码",90); vector<Student> stuTable1; stuTable1.push_back(stu1); stuTable1.push_back(stu2); stuTable1.push_back(stu3); Student stu4(10001, "李三",70); Student stu5(10002, "张四",80); Student stu6(10003, "刘码",90); vector<Student> stuTable2; stuTable2.push_back(stu4); stuTable2.push_back(stu5); stuTable2.push_back(stu6); pair<vector<Student>::iterator, vector<Student>::iterator> result; result = mismatch(stuTable1.begin(), stuTable1.end(), stuTable2.begin()); cout << "第一个成绩不等的学生为" << result.first->name() << "/t" << result.second->name() << endl; system("pause"); return 0; }