Converting a function to an operator
Apr 30, 2017 at 6:17pm UTC
How to convert the function of combining two sets into a class operator?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void Union(int n, int first[], int second[]) // объединение множеств
{
vector<int > v(10); // 0 0 0 0 0 0 0 0 0 0
vector<int >::iterator it;
sort(first, first + n);
sort(second, second + n);
it = set_union(first, first + n, second, second + n, v.begin());
v.resize(it - v.begin());
cout << "The union has " << (v.size()) << " elements:\n" ;
for (it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
cout << '\n' ;
}
int main()
{
int n;
cout << "Enter the dimension of the set: " ;
cin >> n;
int *first = new int [n];
int *second = new int [n];
cout << "Enter the first set: " ;
for (int i = 0; i < n; i++)
{
cin >> first[i];
}
cout << "Enter the second set: " ;
for (int i = 0; i < n; i++)
{
cin >> second[i];
}
Union(n, first, second);
system("pause" );
return 0;
}
Apr 30, 2017 at 6:55pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct setUnion
{
std::vector<int > m_container;
size_t m_dim;
setUnion(const size_t dim) : m_dim(dim)
{
for (auto i = 0; i < dim; ++i)
{
int num{};
std::cout << "Enter number " << i + 1 << " of " << dim << "\n" ;
std::cin >> num;
m_container.push_back(num);
}
}
void operator + (setUnion& rhs);
};
int main()
{
setUnion s1(5);
setUnion s2(10);
s1 + s2;
}
void setUnion::operator + (setUnion& rhs)
{
vector<int > v(m_container.size()+rhs.m_container.size());
vector<int >::iterator it;
std::sort (m_container.begin(), m_container.end());
std::sort(rhs.m_container.begin(), rhs.m_container.end());
it = set_union(m_container.begin(), m_container.end(), rhs.m_container.begin(), rhs.m_container.end(), v.begin());
v.resize(it - v.begin());
cout << "The union has " << (v.size()) << " elements:\n" ;
for (it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
cout << '\n' ;
}
Last edited on Apr 30, 2017 at 6:57pm UTC
May 1, 2017 at 2:23am UTC
updated the operator + overload to support concatenation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <iostream>
#include <vector>
#include <algorithm>
struct setUnion
{
std::vector<int > m_container;
size_t m_dim;
setUnion(const std::vector<int >& container)
: m_container(container), m_dim(container.size()){}
setUnion(const size_t dim) : m_dim(dim)
{
for (auto i = 0; i < dim; ++i)
{
int num{};
std::cout << "Enter number " << i + 1 << " of " << dim << "\n" ;
std::cin >> num;
m_container.push_back(num);
}
}
setUnion operator + (setUnion& rhs);
};
std::ostream& operator << (std::ostream& os, const setUnion& s)
{
for (const auto & elem : s.m_container)os << elem << " " ; os << "\n" ;
return os;
}
int main()
{
setUnion s1(2);
setUnion s2(3);
setUnion s3(4);
auto s4 = s1 + s2 + s3;
std::cout << s4;
}
setUnion setUnion::operator + (setUnion& rhs)
{
std::vector<int > v(m_container.size()+rhs.m_container.size());
std::vector<int >::iterator it;
std::sort (m_container.begin(), m_container.end());
std::sort(rhs.m_container.begin(), rhs.m_container.end());
it = set_union(m_container.begin(), m_container.end(), rhs.m_container.begin(), rhs.m_container.end(), v.begin());
v.resize(it - v.begin());
return setUnion(v);
}
Topic archived. No new replies allowed.