I have this .cpp file and I have to write the methods set_union, subset, complement, and contains. My two problem is first: How can I have a complement without a universal set and how will ("Hi","Hello","Aloha","Ciao")
sets complement give "C++"
as a member.
Second with the set a and x it gives me an error :
no instance of constructor "set_operations<T>::set_operations [with T=std::string]" matches the argument list -- argument types are: (std::set<std::string, string_size_less, std::allocator<std::string>>)
and
no suitable user-defined conversion from "std::set<std::string, std::greater< std::string>, std::allocator< std::string>>" to "std::set<std::string, std::less< std::string>, std::allocator< std::string>>" exists
How can I get the sets comparison as function parameter?
main.cpp:
#include <iostream>
#include "setops.h"
#include <set>
#include "setops.h"
#include <algorithm>
#include <iterator>
#include <string>
#include <numeric>
struct string_size_less
{
bool operator()(const std::string &a,
const std::string &b)
{
return a.size() < b.size();
}
};
const int max = 1000;
bool check()
{
std::set<int> si;
for (int i = 0; i < max; ++i)
{
si.insert(i);
}
std::set<std::string> msgs;
msgs.insert("Hi");
msgs.insert("Hello");
msgs.insert("Ciao");
msgs.insert("Aloha");
set_operations<std::string> ops(msgs);
const set_operations<std::string> cops(msgs);
set_operations<int> opi(si);
for (int i = 0; i < max; ++i)
{
opi.complement();
}
ops.complement();
if (!cops.contains("Hi") || !opi.contains(max / 2) ||
ops.contains("Ciao") || ops.contains("Aloha") ||
opi.contains(2 * max) || cops.contains("?") ||
cops.contains("NullPointerException") || !ops.contains("C++"))
{
return false;
}
std::set<std::string> w;
w.insert(":-)");
w.insert(":-P");
w.insert("Ciao");
ops.set_union(w);
std::set<int> nums;
nums.insert(max / 2);
nums.insert(max / 3);
nums.insert(max);
opi.complement();
opi.set_union(nums);
opi.complement();
if (opi.contains(max / 2) || !opi.contains(max / 4) ||
!ops.subset(w) || cops.subset(w) ||
ops.contains("Hi") || !ops.contains("Ciao") ||
!ops.contains(":-P") || cops.contains(":-P"))
{
ops.complement();
return false;
}
std::set<std::string, string_size_less> x;
x.insert("Hello");
x.insert("Ciao");
std::set<std::string, std::greater<std::string>> a;
a.insert(":-o");
set_operations<std::string> m(x);
m.set_union(a);
return (!m.contains(":-/") && !m.subset(msgs));
}
int main()
{
std::cout
<< "Your solution is "
<< (check() ? "" : "not ")
<< "ready for submission."
<< std::endl;
}
setops.h:
#ifndef SET
#define SET
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <set>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <iterator>
#include <string>
#include <numeric>
class Comp
{
bool comp;
};
template <typename T>
class set_operations
{
private:
std::set<T> data; // data
public:
set_operations::set_operations(const std::set<T> &s) : data(s.begin(), s.end()) {}
void complement();
bool contains(T search) const;
void set_union(std::set<T> set2);
bool subset(std::set<T> set2) const;
void write();
set_operations &operator+=(const std::set<T> &set)
{
typename std::set<T>::iterator itr = set.begin();
for (; itr != set.end(); ++itr)
{
this->data.insert(*itr);
};
return *this;
}
};
template <typename T>
bool set_operations<T>::contains(T search) const
{
bool ret = false;
typename std::set<T>::iterator itr = data.begin();
for (; itr != data.end(); ++itr)
{
if (*itr == search)
{
ret = true;
}
}
return ret;
}
template <typename T>
void set_operations<T>::set_union(std::set<T> set2)
{
typename std::set<T>::iterator itr = set2.begin();
for (; itr != set2.end(); ++itr)
{
data.insert(*itr);
}
}
template <typename T>
bool set_operations<T>::subset(std::set<T> set2) const
{
set_operations<T> test(data);
bool ret = true;
typename std::set<T>::iterator itr = set2.begin();
for (; itr != set2.end(); ++itr)
{
if (!test.contains(*itr))
{
ret = false;
}
}
return ret;
}
template <typename T>
void set_operations<T>::write()
{
typename std::set<T>::iterator itr = data.begin();
for (; itr != data.end(); ++itr)
{
std::cout << *itr << std::endl;
}
};
#endif
Aucun commentaire:
Enregistrer un commentaire