C/C++

С++. Почему в методе set_union единица повторяется несколько раз? Код и скриншот ниже

Код:
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <iterator>

using namespace std;

template <typename T>
void print (const T& a) {
for (const auto& e : a) cout << e << ' ';
cout << endl;
}

void example_vector ()
{
int p,n,m;
cin >> n;
cin >> m;
vector<int> v1(n);
for (int i=0; i< v1.size(); i++){
cin >> v1[i];
}
vector<int> v2(m);
for (int j=0; j< v2.size(); j++){
cin >> v2[j];
}

vector<int> v3, v4, v5, v6;

set_union (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v3));
set_intersection (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v4));

set_difference (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v5));
set_symmetric_difference (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v6));
p=v4.size();
cout << "INTERSECTION: ";
cout << p;
cout << endl;
cout << "set_union: "; print (v3);
cout << "set_intersection: "; print (v4);
cout << "set_difference: "; print (v5);
cout << "set_symmetric_difference: "; print (v6);

cout << endl;
}

int main () {

example_vector ();
return 0;
}
Потому что метод set_union объединяет два контейнера в один.

Вот вам про операции над множествами. Подтяните математику.
https://ru.wikipedia.org/wiki/Множество#Операции_над_множествами
AA
Abdimalik Altybaev
74 848
Лучший ответ
Второй массив отсортирован по "greater" а для обьединения используется "less" по умолчанию. Для правильного обьединения сортировки обоих массивов должны быть одинаково направлены и совпадать с типом сортировки set_union.
Андрей Галах
Андрей Галах
51 416
С++. Почему в методе set_union единица повторяется несколько раз? Код и скриншот ниже
Код:
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <iterator>

using namespace std;

template <typename T>
void print (const T& a) {
for (const auto& e : a) cout << e << ' ';
cout << endl;
}

void example_vector ()
{
int p,n,m;
cin >> n;
cin >> m;
vector<int> v1(n);
for (int i=0; i< v1.size(); i++){
cin >> v1[i];
}
vector<int> v2(m);
for (int j=0; j< v2.size(); j++){
cin >> v2[j];
}

vector<int> v3, v4, v5, v6;

set_union (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v3));
set_intersection (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v4));

set_difference (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v5));
set_symmetric_difference (v1.begin (), v1.end (), v2.begin (), v2.end (), back_inserter (v6));
p=v4.size();
cout << "INTERSECTION: ";
cout << p;
cout << endl;
cout << "set_union: "; print (v3);
cout << "set_intersection: "; print (v4);
cout << "set_difference: "; print (v5);
cout << "set_symmetric_difference: "; print (v6);

cout << endl;
}

int main () {

example_vector ();
return 0;
}