STL标准库算法(下)排序算法 set_difference,set_union,set_symmetric_difference等函数详细解释

该篇博客接上篇博客继续详细解释STL中的排序算法中的set类型的函数怎么进行具体应用废

话不多说,我们来直接看代码段吧,代码段中有对各个算法的详细的解释,请细看。


#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void output(int val)
{
    cout << val << " ";
}

int main()
{
    vector<int> ivc1, ivc2, ivc3;
    int n;
    cout << "请输入ivc1的元素个数:" << endl;
    cin >> n;
    cout << "请输入ivc1各个元素的值:" << endl;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        ivc1.push_back(x);
    }
    cout << "请输入ivc2的元素个数:" << endl;
    cin >> n;
    cout << "请输入ivc2各个元素的值:" << endl;
    for (int i = 0; i < n; i++)
    {
        int x;
        cin >> x;
        ivc2.push_back(x);
    }
    cout << endl;
    cout << "ivc1:" << endl;
    for_each(ivc1.begin(), ivc1.end(), output);
    cout << endl;
    cout << "ivc2:" << endl;
    for_each(ivc2.begin(), ivc2.end(), output);
    cout << endl;
    cout << endl;


    /*
        next_permutation(first,last)
        获得下一个排列组合,可以遍历全排列,
        next_permutation(数组头地址,数组尾地址);若下一个排列存在,则返回真,如果不存在则返回假。
    */
    cout << "After next_permutation():" << endl;
    cout << "数组a的全排列:" << endl;
    int a[] = { 1,5,24 };
    for (int i = 0; i < 3; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    while (next_permutation(a, a + 3))
    {
        for (int i = 0; i < 3; i++)
            cout << a[i] << " ";
        cout << endl;
    }

    /*
        nth_element(first,nth,last)
        重新排序列中第n各元素的左右两端
        通过调用nth_element(start, start + n, end)来找到这个数组中中位数的位置。
        可以理解为从start到n, 从n到end分为两个区间start到n都是小于n的,n到end都是大于n的。
    */
    int b[8] = { 87,24,6,7,1,38,4,45 };
    cout << "数组b原排序:" << endl;
    for (int i = 0; i < 8; i++)
        cout << b[i] << " ";
    cout << endl;
    cout << endl;
    cout << "After nth_element(b,b+4,b+8):" << endl;
    nth_element(b, b + 4, b + 8);
    cout << "第二大的是:" << b[6] << endl;
    for (int i = 0; i < 8; i++)
        cout << b[i] << " ";
    cout << endl;
    cout << endl;
    
    /*
        partial_sort_copy(first,last,first2,last2)
        局部排序并复制到其他位置。
        将first和last的内容复制的同时并且排序存储在first2和last2中。
    */
    cout << "After partial_sort_copy():" << endl;
    ivc3.resize(ivc1.size());
    partial_sort_copy(ivc1.begin(), ivc1.end(), ivc3.begin(), ivc3.end());
    for_each(ivc3.begin(), ivc3.end(), output);
    cout << endl;
    cout << endl;

    /*
        partial_sort(first,middle,last)
        first是起始位置,middle是中间的位置,last是最后的位置
        也就是对middle两边的元素进行排序
        只对前半部分排序,还可以加一个参数,less或greater,具体实现看如下
        后半部分的是随机排序,不一定是正常排序
        注意如果没有后面的less/greater系统默认为less<>()
    */
    cout << "After partial_sort():" << endl;
    partial_sort(ivc2.begin(), ivc2.begin() + 4, ivc2.end(),less<>());
    cout << "partial_sort(less)后的ivc2:" << endl;
    for_each(ivc2.begin(), ivc2.end(), output);
    cout << endl;
    cout << endl;
    cout << "partial_sort(greater)后的ivc2:" << endl;
    partial_sort(ivc2.begin(), ivc2.begin() + 4, ivc2.end(), greater<>());
    for_each(ivc2.begin(), ivc2.end(), output);
    cout << endl;
    cout << endl;

    /*
        prev_permutation(first,last)
        改变迭代器first和last指明范围内的元素排序,使新排列是下一个比原排列小的排列。
    */
    cout << "After prev_permuatation();" << endl;
    prev_permutation(ivc1.begin(), ivc1.end());
    for_each(ivc1.begin(), ivc1.end(), output);
    cout << endl;
    cout << endl;

    /*
        set类的函数,返回值都是一个迭代器
        set_fifference(first,last,first2,last2,result)
        求差集,前后顺序不同结果也不会相同
        set_intersection(first,last,first2,last2,result)
        求出交集
        set_symmetric_difference(first,last,first2,last2,result)
        求对称差集
        setunion(first,last,first2,last2,result)
        求出有序的并集
    */
    sort(ivc1.begin(), ivc1.end());
    sort(ivc2.begin(), ivc2.end());
    vector<int>::iterator it = set_difference(ivc1.begin(), ivc1.end(), ivc2.begin(), ivc2.end(), ivc3.begin());
    cout << "After set_difference():" << endl;
    cout << "ivc1和ivc2的差集:" << endl;    //ivc1有的元素ivc2没有的元素
    for_each(ivc3.begin(), it, output);
    cout << endl;
    cout << "ivc2和ivc1的差集:" << endl;    //ivc2有的元素ivc1没有的元素
    it = set_difference(ivc2.begin(), ivc2.end(), ivc1.begin(), ivc1.end(), ivc3.begin());
    for_each(ivc3.begin(), it, output);
    cout << endl;
    cout << endl;

    vector<int> ivc4;
    ivc4.resize(min(ivc1.size(), ivc2.size()));
    it = set_intersection(ivc1.begin(), ivc1.end(), ivc2.begin(), ivc2.end(), ivc4.begin());
    cout << "After set_intersection():" << endl;
    cout << "ivc1和ivc2的交集:" << endl;
    for_each(ivc4.begin(), it, output);
    cout << endl;
    cout << endl;

    cout << "After set_union():" << endl;
    cout << "ivc1和ivc2的并集:" << endl;
    vector<int> ivc5;
    ivc5.resize(ivc1.size() + ivc2.size());
    set_union(ivc1.begin(), ivc1.end(), ivc2.begin(), ivc2.end(), ivc5.begin());
    for_each(ivc5.begin(), ivc5.end(), output);
    cout << endl;
    cout << endl;

    cout << "After set_symmetric_difference():" << endl;
    cout << "ivc1和ivc2的对称差集:" << endl;
    vector<int> ivc6;
    ivc6.resize(10);
    set_symmetric_difference(ivc1.begin(), ivc1.end(), ivc2.begin(), ivc2.end(), ivc6.begin());
    for_each(ivc6.begin(), ivc6.end(), output);
    cout << endl;
    cout << endl;
    return 0;
}

运行结果如下图所示:

 注意:每一个存储的容器都需要定义容器的大小,否则容器中将不会正确的存储所想要的数据,注意观看每一个函数的返回值,返回值出错时,系统会直接进行报错。


 set_difference(ivc1,ivc2)

            

 ivc1和ivc2的差集

set_difference(ivc2,ivc1)

    

 ivc2和ivc1的差集 

set_intersection(ivc1,ivc2)

             

 ivc1和ivc2的交集

set_union(ivc1,ivc2)

             

 ivc1和ivc2的并集

set_symmetric_difference(ivc1,ivc2)

         

ivc1和ivc2的对称差集

这就是今天所讲述的内容,具体的内容详细解释都在代码段里请想要了解的人细看代码段!!!

猜你喜欢

转载自blog.csdn.net/m0_61886762/article/details/124392853