STL_Algorithm10: Set--集合操作

#include<iostream>

#include<algorithm>

#include<iterator>

 

using std::cout;

using std::endl;

 

int main()

{

    const int SIZE1 = 10, SIZE2 = 5, SIZE3 = 20;

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

    int a2[SIZE2] = { 4, 5, 6, 7, 8 };

    int a3[SIZE2] = { 4, 5, 6, 11, 15 };

 

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

 

    cout<<"a1 contains: ";

    std::copy( a1, a1 + SIZE1, outputIt );

    cout<<"/na2 contains: ";

    std::copy( a2, a2 + SIZE2, outputIt );

    cout<<"/na3 contains: ";

    std::copy( a3, a3 + SIZE2, outputIt );

 

    if( std::includes( a1, a1 + SIZE1, a2, a2 + SIZE2 ) )

        cout<<"/n/na1 includes a2";

    else

        cout<<"/n/na2 does not includes a2";

 

    if( std::includes( a1, a1 + SIZE1, a3, a3 + SIZE2 ) )

        cout<<"/n/na1 includes a3";

    else

        cout<<"/n/na1 does not includes a3";

 

    int difference[SIZE1];

 

    int* ptr = std::set_difference( a1, a1 + SIZE1, a2, a2 + SIZE2, difference );

 

    cout<<"/n/nset_difference of a1 and a2 is: ";

    std::copy( difference, ptr, outputIt );

 

    int intersection[SIZE1];

 

    ptr = std::set_intersection( a1, a1 + SIZE1, a2, a2 + SIZE2, intersection );

 

    cout<<"/n/nset_intersection of a1 and a2 is: ";

    std::copy( intersection, ptr, outputIt );

 

    int symmetric_difference[ SIZE1 ];

 

    ptr = std::set_symmetric_difference( a1, a1 + SIZE1, a3, a3 + SIZE2, symmetric_difference );

 

    cout<<"/n/nset_symmetric_difference of a1 and a3 is: ";

    std::copy( symmetric_difference, ptr, outputIt );

 

    int unionSet[ SIZE3 ];

 

    ptr = std::set_union( a1, a1 + SIZE1, a3, a3 + SIZE2, unionSet );

 

    cout<<"/n/nset_union of a1 and a3 is: ";

    std::copy( unionSet, ptr, outputIt );

 

    cout<<endl;

 

    return 0;

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值