STL standard library algorithm (below) sorting algorithm set_difference, set_union, set_symmetric_difference and other functions in detail

This blog follows the previous blog and continues to explain in detail how the set type functions in the sorting algorithm in STL are used for specific applications.

Not much to say, let's look at the code segment directly. There is a detailed explanation of each algorithm in the code segment, please read it carefully.


#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;
}

The running result is shown in the figure below:

 Note: Each storage container needs to define the size of the container, otherwise the desired data will not be stored correctly in the container. Pay attention to the return value of each function. When the return value is wrong, the system will directly report an error.


 set_difference(ivc1,ivc2)

            

 The difference between ivc1 and ivc2

set_difference(ivc2,ivc1)

    

 The difference between ivc2 and ivc1 

set_intersection(ivc1,ivc2)

             

 intersection of ivc1 and ivc2

set_union(ivc1,ivc2)

             

 Union of ivc1 and ivc2

set_symmetric_difference(ivc1,ivc2)

         

Symmetric difference of ivc1 and ivc2

This is what I will talk about today. The detailed explanation of the specific content is in the code segment, please read the code segment carefully for those who want to know! ! !

Guess you like

Origin blog.csdn.net/m0_61886762/article/details/124392853