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

📅  最后修改于: 2023-12-03 15:37:34.745000             🧑  作者: Mango

在C++中使用STL合并操作

STL是C++中一个非常强大的库。其中包含了许多有用的算法和数据结构。在本文中,我们将介绍如何使用STL中的合并操作。

merge()

merge()可以将两个已排序的数组合并为一个有序的数组。其函数原型如下:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt merge(InputIt1 first1, InputIt1 last1,
                InputIt2 first2, InputIt2 last2,
                OutputIt d_first);
  • first1last1是第一个数组的起始位置和结束位置,包括第一个数组的元素;
  • first2last2是第二个数组的起始位置和结束位置,包括第二个数组的元素;
  • d_first是合并后数组的起始位置。

例如,下面的代码将两个已排序的数组arr1arr2合并为一个有序的数组result

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int arr1[] = {1, 3, 5, 7, 9};
    int arr2[] = {2, 4, 6, 8, 10};
    vector<int> result(10);
    merge(arr1, arr1+5, arr2, arr2+5, result.begin());
    for(int i=0; i<10; i++) cout << result[i] << " ";
    cout << endl;
    return 0;
}

输出结果为:

1 2 3 4 5 6 7 8 9 10
includes()

includes()可以判断一个已排序的数组是否包含另一个已排序的数组。其函数原型如下:

template<class InputIt1, class InputIt2>
bool includes(InputIt1 first1, InputIt1 last1,
              InputIt2 first2, InputIt2 last2);
  • first1last1是第一个数组的起始位置和结束位置,包括第一个数组的元素;
  • first2last2是第二个数组的起始位置和结束位置,包括第二个数组的元素。

例如,下面的代码将判断一个已排序的数组arr是否包含另一个已排序的数组subset

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int arr[] = {1, 2, 3, 4, 5};
    int subset[] = {2, 3, 4};
    bool result = includes(arr, arr+5, subset, subset+3);
    if(result) cout << "arr contains subset!" << endl;
    else cout << "arr does not contain subset!" << endl;
    return 0;
}

输出结果为:

arr contains subset!
set_union()

set_union()可以将两个已排序的数组取并集。其函数原型如下:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_union(InputIt1 first1, InputIt1 last1,
                    InputIt2 first2, InputIt2 last2,
                    OutputIt d_first);
  • first1last1是第一个数组的起始位置和结束位置,包括第一个数组的元素;
  • first2last2是第二个数组的起始位置和结束位置,包括第二个数组的元素;
  • d_first是取并集后数组的起始位置。

例如,下面的代码将两个已排序的数组arr1arr2取并集为一个有序的数组result

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int arr1[] = {1, 3, 5, 7, 9};
    int arr2[] = {2, 4, 6, 8, 10};
    vector<int> result(10);
    set_union(arr1, arr1+5, arr2, arr2+5, result.begin());
    for(int i=0; i<10; i++) cout << result[i] << " ";
    cout << endl;
    return 0;
}

输出结果为:

1 2 3 4 5 6 7 8 9 10
set_intersection()

set_intersection()可以将两个已排序的数组取交集。其函数原型如下:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
                            InputIt2 first2, InputIt2 last2,
                            OutputIt d_first);
  • first1last1是第一个数组的起始位置和结束位置,包括第一个数组的元素;
  • first2last2是第二个数组的起始位置和结束位置,包括第二个数组的元素;
  • d_first是取交集后数组的起始位置。

例如,下面的代码将两个已排序的数组arr1arr2取交集为一个有序的数组result

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {2, 3, 4, 6, 8};
    vector<int> result(5);
    set_intersection(arr1, arr1+5, arr2, arr2+5, result.begin());
    for(int i=0; i<result.size(); i++) cout << result[i] << " ";
    cout << endl;
    return 0;
}

输出结果为:

2 3 4
set_difference()

set_difference()可以将两个已排序的数组取差集。其函数原型如下:

template<class InputIt1, class InputIt2, class OutputIt>
OutputIt set_difference(InputIt1 first1, InputIt1 last1,
                          InputIt2 first2, InputIt2 last2,
                          OutputIt d_first);
  • first1last1是第一个数组的起始位置和结束位置,包括第一个数组的元素;
  • first2last2是第二个数组的起始位置和结束位置,包括第二个数组的元素;
  • d_first是取差集后数组的起始位置。

例如,下面的代码将两个已排序的数组arr1arr2取差集为一个有序的数组result

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {2, 3, 4};
    vector<int> result(5);
    set_difference(arr1, arr1+5, arr2, arr2+3, result.begin());
    for(int i=0; i<result.size(); i++) cout << result[i] << " ";
    cout << endl;
    return 0;
}

输出结果为:

1 5 0 0 0
inplace_merge()

inplace_merge()可以对一个已排序数组进行原地合并。其函数原型如下:

template<class BidirIt>
void inplace_merge(BidirIt first, BidirIt middle, BidirIt last);
  • firstlast是数组的起始位置和结束位置,包括数组的元素;
  • middle是数组的中间位置。

例如,下面的代码对一个已排序数组arr进行原地合并:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int arr[] = {1, 3, 5, 2, 4, 6};
    inplace_merge(arr, arr+3, arr+6);
    for(int i=0; i<6; i++) cout << arr[i] << " ";
    cout << endl;
    return 0;
}

输出结果为:

1 2 3 4 5 6

以上就是使用STL中的合并操作的介绍。务必注意每个函数的具体用法、参数和返回值。