c++ 判断两个容器是否相等(equal)


 

#include <iostream>     // cout
#include <algorithm>    // equal
#include <vector>       // vector
using namespace std;
bool mypredicate (int i, int j) {
    return (i==j);
}

int main () {
    int myints[] = {20,40,60,80,100};               //   myints: 20 40 60 80 100
    vector<int>myvector (myints,myints+5);     // myvector: 20 40 60 80 100
    
    // using default comparison:
    if ( equal (myvector.begin(), myvector.end(), myints) )
        cout << "The contents of both sequences are equal.\n";
    else
        cout << "The contents of both sequences differ.\n";
    
    myvector[3]=81;                                 // myvector: 20 40 60 81 100
    
    // using predicate comparison:
    if ( equal (myvector.begin(), myvector.end(), myints, mypredicate) )
        cout << "The contents of both sequences are equal.\n";
    else
        cout << "The contents of both sequences differ.\n";
    
    return 0;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM