两个排序范围的对称差
两个集合之间的对称差异是由其中一个集合中存在但另一个集合中不存在的元素形成的。在每个范围内的等效元素中,被丢弃的是那些在调用之前以现有顺序出现的元素。复制的元素的现有顺序也会保留。
第一个版本使用operator< 来比较元素,第二个版本使用comp 来比较元素。如果 (!(a<b) && !(b<a)) 或 if (!comp(a, b) && !comp(b, a)),两个元素 a 和 b 被视为等效。
范围内的元素应已排序。
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
// CPP program to illustrate
// std :: set_symmetric_difference
#include <bits/stdc++.h>
using namespace std;
int main()
{
// students attending first class
std::vector<string> class1{ "Samir", "Manoj", "Pranav", "Rajesh" };
// students attending second class
std::vector<string> 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<string> result(10);
std::vector<string>::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<string>::iterator i = result.begin(); i != it; i++) {
cout << *i << " ";
}
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. 使用自定义函数:
句法:
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
输出:
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
// CPP program to illustrate
// std :: set_symmetric_difference
#include <bits/stdc++.h>
using namespace std;
int main()
{
// students attending first class
std::vector<string> class1{ "Samir", "Manoj", "Pranav", "Rajesh" };
// students attending second class
std::vector<string> 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<string> result(10);
std::vector<string>::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<string>::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++ setlocale()用法及代码示例
- C++ setbuf()用法及代码示例
- C++ setvbuf()用法及代码示例
- C++ set begin()用法及代码示例
- C++ set swap()用法及代码示例
- C++ set size()用法及代码示例
- C++ set rend()用法及代码示例
- C++ set cbegin()用法及代码示例
- C++ set cend()用法及代码示例
- C++ set rbegin()用法及代码示例
- C++ set key_comp()用法及代码示例
- C++ set clear()用法及代码示例
- C++ set erase()用法及代码示例
- C++ set end()用法及代码示例
- C++ set empty()用法及代码示例
- C++ set crend()用法及代码示例
- C++ set emplace()用法及代码示例
- C++ set crbegin()用法及代码示例
- C++ set begin用法及代码示例
- C++ set cbegin用法及代码示例
- C++ set end用法及代码示例
- C++ set cend用法及代码示例
- C++ set rbegin用法及代码示例
- C++ set crbegin用法及代码示例
- C++ set rend用法及代码示例
注:本文由纯净天空筛选整理自佚名大神的英文原创作品 set_symmetric_difference in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。