C++ C++;:战略模式+;每个策略的迭代器
假设我有一个策略模式,有两个类实现该策略,其中每个类都有一些值集,但是这些值存储在不同的容器中,比如向量和列表 我希望有一个迭代器,允许我遍历每个类中的每个值。 我还希望能够使用标准迭代器方式,这意味着以这种方式检查值:C++ C++;:战略模式+;每个策略的迭代器,c++,strategy-pattern,C++,Strategy Pattern,假设我有一个策略模式,有两个类实现该策略,其中每个类都有一些值集,但是这些值存储在不同的容器中,比如向量和列表 我希望有一个迭代器,允许我遍历每个类中的每个值。 我还希望能够使用标准迭代器方式,这意味着以这种方式检查值: Strategy s = //Some initialization. Strategy :: iterator it; for (it = s.begin(); it != s.end() ; ++it){ //Do something with *it. } 目前我唯一的
Strategy s = //Some initialization.
Strategy :: iterator it;
for (it = s.begin(); it != s.end() ; ++it){
//Do something with *it.
}
目前我唯一的解决方案是:另一种策略模式,其中strategy类具有上下文迭代器。另一个strategyIterator和每个“strategy实现类”都有自己的类自迭代器:public strategyIterator
但这似乎很麻烦,也许其他人比我更有创造力。
所以我真的很感激一些想法:),谢谢
它适用于处理多项式的程序,其中策略是多项式的表示,一个是向量,另一个是列表
下面是我要使用的程序的H文件:
#include <iostream>
#include <vector>
#include <list>
using namespace std;
#ifndef MYPOLY_H
#define MYPOLY_H
class PolyRep;
class RegIterator;
typedef enum Poly
{
REG_POLY = 1, SPARSE_POLY = 2
};
/* =============================================================================
* MyPoly class.
* =============================================================================*/
class MyPoly
{
public:
MyPoly(double arr[], unsigned int arrSize);
MyPoly(double num);
MyPoly(double X[], double Y[], int arrSize);
MyPoly(string sPoly);
~MyPoly();
string toString();
double evaluate(double x) const;
MyPoly derive(int n);
MyPoly & operator =(const MyPoly &rhs);
MyPoly & operator +=(const MyPoly &rhs);
MyPoly & operator -=(const MyPoly &rhs);
MyPoly & operator *=(const MyPoly &rhs);
MyPoly operator +(const MyPoly& other);
MyPoly operator -(const MyPoly& other);
MyPoly operator *(const MyPoly& other);
bool operator ==(const MyPoly& b) const;
MyPoly operator -() const;
private:
PolyRep * _poly;
};
/* =============================================================================
* PolyRep class.
* =============================================================================*/
class PolyRep
{
public:
virtual double evaluate(const double &x) const = 0;
//virtual iterator begin () = 0;
//virtual PolyRep * derive(int n) = 0;
protected:
int _degree;
};
/* =============================================================================
* RegPoly class.
* =============================================================================*/
class RegPoly : public PolyRep
{
public:
RegPoly(double arr[], int degree);
~RegPoly();
double evaluate(const double &x) const;
private:
vector <double> _values;
};
/* =============================================================================
* SparsePoly class.
* =============================================================================*/
class SparsePoly : public PolyRep
{
public:
SparsePoly(double arr[], int degree);
~SparsePoly();
double evaluate(const double &x) const;
private:
/*A class to represent a node in the SaprsePoly list.*/
class SparseNode
{
public:
SparseNode(int n = 0, double value = 0);
int getN() const;
double getValue() const;
private:
int _n;
double _value;
};
/*End of sparse node class.*/
list <SparseNode*> _values;
};
#endif /* MYPOLY_H */
#包括
#包括
#包括
使用名称空间std;
#ifndef MYPOLY_H
#定义MYPOLY_H
PolyRep类;
类注册器;
typedef枚举多边形
{
REG_POLY=1,稀疏_POLY=2
};
/* =============================================================================
*MyPoly类。
* =============================================================================*/
类MyPoly
{
公众:
MyPoly(双arr[],无符号整数arrSize);
MyPoly(双num);
MyPoly(双X[],双Y[],整数arrSize);
MyPoly(stringspoly);
~MyPoly();
字符串toString();
双评价(双x)常数;
MyPoly派生(int-n);
MyPoly和运算符=(常量MyPoly和rhs);
MyPoly和运算符+=(常量MyPoly和rhs);
MyPoly和运算符-=(常数MyPoly和rhs);
MyPoly和运算符*=(常数MyPoly和rhs);
MyPoly运算符+(常量MyPoly和其他);
MyPoly运算符-(常量MyPoly和其他);
MyPoly运算符*(常数MyPoly和其他);
布尔运算符==(常数MyPoly&b)常数;
MyPoly算子-()常数;
私人:
PolyRep*\u poly;
};
/* =============================================================================
*PolyRep类。
* =============================================================================*/
类PolyRep
{
公众:
虚拟双重评估(constdouble&x)const=0;
//虚拟迭代器begin()=0;
//虚拟PolyRep*派生(int n)=0;
受保护的:
国际学位;
};
/* =============================================================================
*RegPoly类。
* =============================================================================*/
类RegPoly:publicpolyrep
{
公众:
RegPoly(双arr[],整数度);
~RegPoly();
双重评估(constdouble&x)const;
私人:
向量_值;
};
/* =============================================================================
*SparsePoly类。
* =============================================================================*/
SparsePoly类:公共PolyRep
{
公众:
SparsePoly(双arr[],int度);
~SparsePoly();
双重评估(constdouble&x)const;
私人:
/*用于表示SAPSEREPLY列表中的节点的类*/
SparseNode类
{
公众:
SparseNode(int n=0,双值=0);
int getN()常量;
双getValue()常量;
私人:
国际;;
双u值;
};
/*稀疏节点类的结束*/
列出(u)值;;
};
#endif/*MYPOLY_H*/
使用名称空间标准在头文件的全局范围内,代码>是应该不惜一切代价避免的。我认为问题不在于list
vsvector
,而在于double
vsSparseNode*
——这将需要模板化,而在运行时无法像您需要的那样解决。不要使用以强调。它们是为编译器使用而保留的。如果要指示成员变量,请切换到u。@chris非常感谢,我刚刚解决了它引起的问题。@Yuushi,值得一提的是,因为很容易使用违反规则的名称,所以这里不适用。仅为全局范围保留一个下划线。有关规则和相关标准参考,请参见。