【发布时间】:2021-03-30 04:45:45
【问题描述】:
我是一名 CS 学生,我有一个需要帮助的任务。 任务是创建一个vectors_predicate_view 类,该类获取一个T 类型参数,一个predictatum 作为模板参数,并且在构造函数中它应该获取2 个std::vector 作为构造函数参数。当范围在视图上时,向量的项目应该根据预测进行排序。如果预测为真,则该项目应放置在构造函数的第一个向量中,如果为假,则应将其他所有内容放置在第二个向量中。如果范围不在视图中,则应将所有内容设置回其原始状态
所以我的vectors_predicate_view 看起来像这样
#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);
}
}
};
主要的不应该修改!!!看起来像这样:
#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;
}
return c;
}
int main()
{
std::cout
<< "Your solution is " << (check() ? "" : "not ")
<< "ready for submission.\n";
}
问题是,如果新声明的vectors_predicate_view 对象在范围内,我如何检查我的类,而不修改main.cpp 中的任何内容?
请看这里的例子:
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;
}
vectors_predicate_view 对象在范围内,但在右花括号之后却不在范围内。 如果对象不再在范围内,我希望能够将所有向量设置回它们在类中的原始状态。
知道我该怎么做吗?
【问题讨论】:
-
我可能误解了您的问题,但是超出范围后,它不再可用。不确定要将类中的向量重置为其原始状态是什么意思...如果您不想修改传递给构造函数的向量,请不要修改它们...
-
听起来您想向
vectors_predicate_view类添加一个析构函数,这将恢复传递给其构造函数的向量的内容(您必须保存指向这些向量的指针)。 -
有什么理由你不能传递向量的副本,所以原件不会被修改?还有boost intrusive container
-
您似乎跳过了一些您可能很明显但其他人不知道的细节。也许更多地关注所需的功能(关于设置向量)而不是你认为应该如何实现该功能?
-
为什么“视图”首先会修改原始向量?