I have a class that looks like this:
#pragma once
#include <vector>
//In this class we add everything to vector1 that gives back true for the predictatum
//everything else goes to vector2
template<typename T, typename Prediktatum>
class vectors_predicate_view
{
private:
Prediktatum predikt;
std::vector<T> originalVector1;
std::vector<T> originalVector2;
public:
vectors_predicate_view(std::vector<T> &vector1, std::vector<T> &vector2)
{
//Putting every element from vector1 into originalVector1
for (auto element : vector1)
originalVector1.push_back(element);
vector1.clear(); //Emptying vector1
//Putting every element from vector2 into originalVector2
for(auto element : vector2)
originalVector2.push_back(element);
vector2.clear(); //Emptying vector2
//We loop through originalVector1
//if the element gives back true for the predictatum, we add it to vector1
//else it goes to vector2
for(auto element : originalVector1)
{
if(predikt(element))
vector1.push_back(element);
else
vector2.push_back(element);
}
//We loop through originalVector2
//if the element gives back true for the predictatum, we add it to vector1
for(auto element : originalVector2)
{
if(predikt(element))
vector1.push_back(element);
}
}
};
and a main that looks like this:
#include <iostream>
#include "vecspred.h"
#include <algorithm>
#include <string>
#include <numeric>
#include <functional>
#include "vecspred.h"
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";
}
};
const int max = 1024;
bool check()
{
bool c = false;
std::vector<int> a;
std::vector<int> b;
for( int i = 0; i < max; ++i )
{
if ( i < max / 2 )
{
a.push_back( i );
}
else
{
b.push_back( i );
}
}
std::vector<std::string> x;
x.push_back( "Cobol" );
x.push_back( "Ada" );
std::vector<std::string> y;
y.push_back( "Javascript" );
y.push_back( "C++" );
y.push_back( "ABAP" );
if ( !c )
{
//vectors_predicate_view is in scope now
vectors_predicate_view<int, is_even> va( a, b );
vectors_predicate_view<std::string, is_good_language> vb( x, y );
c = ( 1 == x.size() && 1 == b[ 0 ] && 2 == a[ 1 ] && "Cobol" == y[ 0 ] );
} //after this bracket it's out of scope
//here every vector should be set back to their original state
if ( !c || "Ada" != x[ 1 ] || "ABAP" != y[ 2 ] || max / 2 != b[ 0 ] )
{
return false;
}
}
int main()
{
std::cout
<< "Your solution is " << (check() ? "" : "not ")
<< "ready for submission.\n";
}
The question is, how can I check in my class if a newly declared vectors_predicate_view object is in scope, without modifying anything in main.cpp?
See for example here:
if ( !c )
{
//vectors_predicate_view is in scope now
vectors_predicate_view<int, is_even> va( a, b );
vectors_predicate_view<std::string, is_good_language> vb( x, y );
c = ( 1 == x.size() && 1 == b[ 0 ] && 2 == a[ 1 ] && "Cobol" == y[ 0 ] );
} //after this bracket it's out of scope
//here every vector should be set back to their original state
if ( !c || "Ada" != x[ 1 ] || "ABAP" != y[ 2 ] || max / 2 != b[ 0 ] )
{
return false;
}
the vectors_predicate_view object is in scope, but after the closing curly bracket it's not. I want to be able to set all the vectors back to their original state in the class, if the object is not in scope anymore.
Any idea how can I do it?
Aucun commentaire:
Enregistrer un commentaire