I'm a CS student, and the I have a task where I need some help. The task is to create a vectors_predicate_view
class that gets a T
type as first - and a predictatum template as a second parameter. In its constructor the class should get 2 std::vector
s as parameters.
Here is the class:
#pragma once #include <vector> template<typename T, typename Predicate> class vectors_predicate_view { private: Predicate predict; std::vector<T> originalContents1; std::vector<T> originalContents2; std::vector<T> &original1; // reference to original vector1 std::vector<T> &original2; // reference to original vector2 void setElements(std::vector<T> &vector1, std::vector<T> &vector2) { //Putting every element from vector1 into originalContents1 for(auto element : vector1) originalContents1.push_back(element); vector1.clear(); //Emptying vector1 //Putting every element from vector2 into originalContents2 for(auto element : vector2) originalContents2.push_back(element); vector2.clear(); //Emptying vector2 //We loop through originalContents1 //if the element gives back true for the predictatum, we add it to vector1 //else it goes to vector2 for(auto element : originalContents1) { if (predict(element)) vector1.push_back(element); else vector2.push_back(element); } //We loop through originalContents2 //if the element gives back true for the predictatum, we add it to vector1 for(auto element : originalContents2) if(predict(element)) vector1.push_back(element); } public: vectors_predicate_view(std::vector<T> &vector1, std::vector<T> &vector2) : original1(vector1), original2(vector2) { setElements(vector1, vector2); } };
The task is to make the second parameter optional, and if it's not given, the class should not do anything with the 2 given vectors inside its constructor.
Just to give an idea, here is how that second template optimally looks like. It's an std::unary_function
that looks like this:
struct is_even: std::unary_function<int, bool> { bool operator()(int i) const { return 0 == i % 2; } }; struct is_good_language: std::unary_function<std::string, bool> { bool operator()(const std::string& s) const { return s == "C++" || s == "C"; } };
and called like this:
const vectors_predicate_view<int, is_even> va(a, b); vectors_predicate_view<std::string, is_good_language> vb(x, y);
Any help would be greatly appreciated!
from Recent Questions - Stack Overflow https://stackoverflow.com/questions/65376868/make-second-template-functional-of-a-unary-function Martin Serdült http://ifttt.com/images/no_image_card.png
没有评论:
发表评论