📜  带有示例的C++中的set_symmetric_difference

📅  最后修改于: 2021-05-30 09:21:27             🧑  作者: Mango

两个排序范围的对称差异
两组中的对称差异是由一组中的元素而不是另一组中的元素形成的。在每个范围的等效元素中,被丢弃的元素是按调用之前的先后顺序出现的元素。对于已复制的元素,还将保留现有顺序。

在第一个版本中使用运算符<比较元素,在第二个版本中使用comp进行比较。如果(!(a

范围内的元素应已订购。

1.使用默认运算符<:

句法 :

Template :
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
                                         InputIterator2 first2, InputIterator2 last2,
                                         OutputIterator result);

Parameters :

first1, last1
Input iterators to the initial and final positions of the first
sorted sequence. The range used is [first1, last1), which contains
all the elements between first1 and last1, including the element
pointed by first1 but not the element pointed by last1.

first2, last2
Input iterators to the initial and final positions of the second
sorted sequence. The range used is [first2, last2).

result
Output iterator to the initial position of the range where the
resulting sequence is stored.
The pointed type shall support being assigned the value of an
element from the other ranges.

comp
Binary function that accepts two arguments of the types pointed
by the input iterators, and returns a value convertible to bool.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

The ranges shall not overlap.

Return Type :
An iterator to the end of the constructed range.
// CPP program to illustrate
// std :: set_symmetric_difference
#include  // std::set_symmetric_difference, std::sort
#include  // std::cout
#include  // std::vector
  
// Driver code
int main()
{
    int first[] = { 5, 10, 15, 20, 25 };
    int second[] = { 50, 40, 30, 20, 10 };
    int n = sizeof(first) / sizeof(first[0]);
  
    // Print first array
    std::cout << "First array contains :";
    for (int i = 0; i < n; i++)
        std::cout << " " << first[i];
    std::cout << "\n";
  
    // Print second array
    std::cout << "Second array contains :";
    for (int i = 0; i < n; i++)
        std::cout << " " << second[i];
    std::cout << "\n\n";
  
    std::vector v(10);
    std::vector::iterator it, st;
  
    // Sorting both the arrays
    std::sort(first, first + 5);
    std::sort(second, second + 5);
  
    // Using default operator<
    it = std::set_symmetric_difference(first, first + 5, 
    second, second + 5, v.begin());
  
    std::cout << "The symmetric difference has "
              << (it - v.begin()) << " elements:\n";
    for (st = v.begin(); st != it; ++st)
        std::cout << ' ' << *st;
    std::cout << '\n';
  
    return 0;
}

输出:

First array contains : 5 10 15 20 25
Second array contains : 50 40 30 20 10

The symmetric difference has 6 elements:
 5 15 25 30 40 50

2.使用自定义函数:

Synatx:

Template :
OutputIterator set_symmetric_difference (InputIterator1 first1, InputIterator1 last1,
                                         InputIterator2 first2, InputIterator2 last2,
                                         OutputIterator result, Compare comp);

Parameters :

first1, last1, first2, last2, result are same as described above.

comp
Binary function that accepts two arguments of the types pointed
by the input iterators, and returns a value convertible to bool.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.

The ranges shall not overlap.

Return Type :
An iterator to the end of the constructed range.
// CPP program to illustrate
// std :: set_symmetric_difference
#include  // std::set_symmetric_difference, std::sort
#include  // std::cout
#include  // std::vector
  
// Custom function
bool comp(int a, int b)
{
    return a < b;
}
  
// Driver code
int main()
{
    int first[] = { 5, 10, 15, 20, 25 };
    int second[] = { 50, 40, 30, 20, 10 };
    int n = sizeof(first) / sizeof(first[0]);
  
    // Print first array
    std::cout << "First array contains :";
    for (int i = 0; i < n; i++)
        std::cout << " " << first[i];
    std::cout << "\n";
  
    // Print second array
    std::cout << "Second array contains :";
    for (int i = 0; i < n; i++)
        std::cout << " " << second[i];
    std::cout << "\n\n";
  
    std::vector v(10);
    std::vector::iterator it, st;
  
    // Sorting both the arrays
    std::sort(first, first + 5);
    std::sort(second, second + 5);
  
    // Using default operator<
    it = std::set_symmetric_difference(first, first + 5,
                                       second, second + 5, v.begin(), comp);
  
    std::cout << "The symmetric difference has "
              << (it - v.begin()) << " elements:\n";
    for (st = v.begin(); st != it; ++st)
        std::cout << ' ' << *st;
    std::cout << '\n';
  
    return 0;
}

输出:

First array contains : 5 10 15 20 25
Second array contains : 50 40 30 20 10

The symmetric difference has 6 elements:
 5 15 25 30 40 50

可能的应用:用于查找一个容器中存在的元素,而不是其他容器中存在的元素。

1.用于查找未同时参加这两个课程的学生列表。这两个班的学生都在列表中。

// CPP program to illustrate
// std :: set_symmetric_difference
#include 
using namespace std;
  
int main()
{
  
    // students attending first class
    std::vector class1{ "Samir", "Manoj", "Pranav", "Rajesh" };
  
    // students attending second class
    std::vector class2{ "Samir", "Junaid", "Manoj", "Pankaj", "Arpit" };
  
    cout << "Students attending first class are : ";
    for (auto i : class1) {
        cout << i << " ";
    }
    cout << "\nStudents attending second class are : ";
    for (auto i : class2) {
        cout << i << " ";
    }
  
    // to store the result of symmetric difference
    std::vector result(10);
  
    std::vector::iterator it;
  
    // finding symmetric difference
    it = set_symmetric_difference(class1.begin(),
                                  class1.end(), class2.begin(), class2.end(), result.begin());
  
    cout << "\n\nList of students that are not taking both classes :";
  
    for (std::vector::iterator i = result.begin(); i != it; i++) {
        cout << *i << " ";
    }
    return 0;
}

输出 :

Students attending first class are : Samir Manoj Pranav Rajesh 
Students attending second class are : Samir Junaid Manoj Pankaj Arpit 

List of students that are not taking both classes :Junaid Pankaj Arpit Pranav Rajesh 

2.它也可以用于从两个列表中查找两个列表中都不存在的数字。
程序在上面给出。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”