STL_Algorithm2: equal, mismatch, lexicographical_compare

#include <iostream>

using std::cout;

using std::endl;

 

#include <algorithm>

#include <vector>

#include <iterator>

 

int main()

{

    const int SIZE = 10;

 

    int a1[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    int a2[SIZE] = {1, 2, 3, 4, 1000, 6, 7, 8, 9, 10 };

 

    std::vector< int > v1( a1, a1 + SIZE );

    std::vector< int > v2( a1, a1 + SIZE );

    std::vector< int > v3( a2, a2 + SIZE );

 

    std::ostream_iterator< int > outputIt( cout, " " );

 

    cout<< "Vector v1 contains: ";

    std::copy( v1.begin(), v1.end(), outputIt );

 

    cout<<"/nVector v2 contains: ";

    std::copy( v2.begin(), v2.end(), outputIt );

 

    cout<<"/nVector v3 contains: ";

    std::copy( v3.begin(), v3.end(), outputIt );

 

    bool result = std::equal( v1.begin(), v1.end(), v2.begin() );

 

    cout<< "/n/nVector v1 "<<( result ? "is":"is not")

        <<" equal to vector v2./n";

 

    result = std::equal( v1.begin(), v1.end(), v3.begin() );

    cout<<"Vector v1 "<<( result ? "is":"is not" )

        << " equal to vector v3./n";

 

    std::pair< std::vector< int >::iterator, std::vector< int >::iterator > location;

 

    location = std::mismatch( v1.begin(), v1.end(), v3.begin() );

 

    cout<<"/nThere is a mismatch between v1 and v3 at "

        << "location "<<(location.first - v1.begin() )

        << "/nwhere v1 contains "<<*location.first

        <<" and v3 contains "<<*location.second

        <<"/n/n";

 

    char c1[SIZE] = "HELLO";

 

    char c2[SIZE] = "BYE BYE";

 

    result = std::lexicographical_compare(c1, c1 + SIZE,  c2, c2 + SIZE );

 

    cout<< c1

        << ( result ? " is less than " : " is greater than or equal to " )

        << c2 <<endl;

 

    return 0;

 

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值