📜  在C++中使用STL合并操作| merge(),includes(),set_union(),set_intersection(),set_difference(),。,inplace_merge,

📅  最后修改于: 2021-04-27 20:06:40             🧑  作者: Mango

C++ STL在头文件“ algorithm”下提供了一些合并操作类,从而以一种简便的方式促进了多个合并操作。
其中一些在下面提到。

  1. merge(beg1,end1,beg2,end2,beg3) :-此函数合并两个已排序的容器,并以已排序的顺序存储在新容器中(合并排序)。它需要5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。
  2. include(beg1,end1,beg2,end2) :-此函数用于检查一个已排序的容器元素是否包括其他已排序的容器元素。如果第一个容器包含第二个容器,则返回true;否则返回false。
    // C++ code to demonstrate the working of 
    // merge() and include()
    #include
    #include // merge operations
    #include // for vector
    using namespace std;
    int main()
    {
     // Initializing 1st vector
     vector v1 = {1, 3, 4, 5, 20, 30};
          
     // Initializing 2nd vector
     vector v2 = {1, 5, 6, 7, 25, 30};
          
     // Declaring resultant vector
     // for mergeing
     vector v3(12);
          
     // Using merge() to merge vectors v1 and v2
     // and storing result in v3
     merge(v1.begin(), v1.end(), v2.begin(), 
                       v2.end(), v3.begin());
          
     // Displaying resultant container
     cout << "The new container after merging is :\n";
     for (int &x : v3) 
         cout << x << " ";
     cout << endl;
       
     // Initializing new vector
     vector v4 = {1, 3, 4, 5, 6, 20, 25, 30};
       
     // Using include() to check if v4 contains v1
     includes(v4.begin(), v4.end(), v1.begin(), v1.end())?
            cout << "v4 includes v1":
            cout << "v4 does'nt include v1";
       
     return 0;    
    }
    

    输出

    The new container after merging is :
    1 1 3 4 5 5 6 7 20 25 30 30 
    v4 includes v1
    
  3. inplace_merge(beg1,beg2,end):-此函数用于在单个容器中对两个连续放置的排序范围进行排序。它需要3个参数,迭代器到第一个排序范围的开头,迭代器到第二个排序范围的开头,迭代器到最后一个位置。
    // C++ code to demonstrate the working of 
    // inplace_merge()
    #include
    #include // merge operations
    #include // for vector
    using namespace std;
    int main()
    {
       
     // Initializing 1st vector
     vector v1 = {1, 3, 4, 5, 20, 30};
          
     // Initializing 2nd vector
     vector v2 = {1, 5, 6, 7, 25, 30};
          
     // Declaring resultant vector
     // for inplace_merge()
     vector v3(12);
       
     // using copy to copy both vectors into 
     // one container
     auto it = copy(v1.begin(), v1.end(), v3.begin());
     copy(v2.begin(), v2.end(), it);
       
     // Using inplace_merge() to sort the container
     inplace_merge(v3.begin(),it,v3.end());
          
     // Displaying resultant container
     cout << "The new container after inplace_merging is :\n";
     for (int &x : v3) 
         cout << x << " ";
     cout << endl;
      
     return 0;
          
    }
    

    输出:

    The new container after inplace_merging is :
    1 1 3 4 5 5 6 7 20 25 30 30 
    
  4. set_union(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的集合并集并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它需要5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。容器应分类,并且有必要将新容器的大小调整为合适的大小。
  5. set_intersection(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的集合交集并将其存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它需要5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。容器应分类,并且有必要将新容器的大小调整为合适的大小。
    在这里可以找到在排序范围内实现集合联合和集合相交的一种方法
    // C++ code to demonstrate the working of 
    // set_union() and set_intersection()
    #include
    #include // for merge operations
    #include // for vector
    using namespace std;
    int main()
    {
     // Initializing 1st vector
     vector v1 = {1, 3, 4, 5, 20, 30};
          
     // Initializing 2nd vector
     vector v2 = {1, 5, 6, 7, 25, 30};
          
     // Declaring resultant vector
     // for union
     vector v3(10);
          
     // Declaring resultant vector
     // for intersection
     vector v4(10);
          
     // using set_union() to compute union  of 2 
     // containers v1 and v2 and store result in v3
     auto it = set_union(v1.begin(), v1.end(), v2.begin(), 
                                  v2.end(), v3.begin());
          
     // using set_intersection() to compute intersection
     // of 2 containers v1 and v2 and store result in v4
     auto it1 = set_intersection(v1.begin(),v1.end(), 
                   v2.begin(), v2.end(), v4.begin());
          
     // resizing new container
     v3.resize(it - v3.begin());
          
     // resizing new container
     v4.resize(it1 - v4.begin());
          
     // Displaying set union
     cout << "Union of two containers is : ";
     for (int &x : v3) 
         cout << x << " ";
     cout << endl;
          
     // Displaying set intersection
     cout << "Intersection of two containers is : ";
     for (int &x : v4) 
         cout << x << " ";
     cout << endl;
          
     return 0;    
    }
    

    输出:

    Union of two containers is : 1 3 4 5 6 7 20 25 30 
    Intersection of two containers is : 1 5 30 
    
  6. set_difference(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的集合差异并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它需要5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。应该对容器进行分类,并且有必要将新容器的大小调整为合适的大小。
  7. s et_symmetric_difference(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的集合对称差并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它需要5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。应该对容器进行分类,并且有必要将新容器的大小调整为合适的大小。
    // C++ code to demonstrate the working of 
    // set_difference() and set_symmetric_difference()
    #include
    #include // for merge operations
    #include // for vector
    using namespace std;
    int main()
    {
     // Initializing 1st vector
     vector v1 = {1, 3, 4, 5, 20, 30};
          
     // Initializing 2nd vector
     vector v2 = {1, 5, 6, 7, 25, 30};
          
     // Declaring resultant vector
     // for difference
     vector v3(10);
          
     // Declaring resultant vector
     // for symmetric_difference
     vector v4(10);
          
          
     // using set_difference() to compute difference
     // of 2 containers v1 and v2.
     auto it = set_difference(v1.begin(), v1.end(),
                  v2.begin(), v2.end(), v3.begin());
          
     // using set_symmetric_difference() to compute 
     // symmetric_difference/ of 2 containers
     auto it1 = set_symmetric_difference(v1.begin(), 
           v1.end(), v2.begin(), v2.end(), v4.begin());
          
     // resizing new container
     v3.resize(it - v3.begin());
          
     // resizing new container
     v4.resize(it1 - v4.begin());
          
     // Displaying set difference
     cout << "Difference of two containers is : ";
     for (int &x : v3) 
         cout << x << " ";
     cout << endl;
          
     // Displaying set symmetric_difference
     cout << "symmetric_difference of two containers is : ";
     for (int &x : v4) 
         cout << x << " ";
     cout << endl;
          
     return 0;    
    }
    

    输出:

    Difference of two containers is : 3 4 20 
    Symmetric difference of two containers is : 3 4 6 7 20 25