有一个泛型类Vector,它扩展了std :: array,还有一个泛型类Expression,用于定义Vectors的可能表达式。
例如:
Vector A({1,2,3});
矢量B({2,2,2});
和表达式:
A + B;
A * B;
A - B;
A / B;
现在我需要这个表达式A ** B,它返回一个double作为两个向量A和B的标量产生,结果必须是:2 + 4 + 6 = 12。问题是运营商的实施** !!!
我该怎么写这个算子**?
我的想法是重载Vector的取消引用运算符*,它返回一个指针,然后重载struct-Mul oder乘法运算符* ...无法解决此错误:
“没有合适的转换函数来自”Expression,Mul,Vector *>“to”double“存在”
template<typename Left, typename Op, typename Right> class Expression {
const Left& m_left;
const Right& m_right;
public:
typedef typename Left::value_type value_type;
// Standard constructor
Expression(const Left& l, const Right& r) : m_left{ l }, m_right{ r } {}
size_t size() const {
return m_left.size();
}
value_type operator[](int i) const {
return Op::apply(m_left[i], m_right[i]);
}
};
struct Mul {
template<typename T> static T apply(T l, T r) {
return l * r;
}
};
template<typename Left, typename Right>
Expression<Left, Mul, Right> operator*(const Left& l, const Right& r) {
return Expression<Left, Mul, Right>(l, r);
}
.......................
// Class Vector extends std::array
template<class T, size_t S> class Vector : public array<T, S> {
public:
// Standard constructor
Vector<T, S>(array<T, S>&& a) : array<T, S>(a) {}
// Initializerlist constructor
Vector(const initializer_list<T>& data) {
size_t s = __min(data.size(), S);
auto it = data.begin();
for (size_t i = 0; i < s; i++)
this->at(i) = *it++;
}
};
.....................................
int main {
Vector<double, 5> A({ 2, 3, 4, 5, 6 });
Vector<double, 5> B({ 3, 3, 3, 3, 3 });
Vector<double, 5> C;
C = A * B; // is a Vector: [6, 9, 12, 15, 18] and it works.
double d = A**B; // but this one does not work, the error message is: "no suitable conversion function from "Expression<Vector<double, 5U>, Mul, Vector<double, 5U> *>" to "double" exists"
cout << d << endl; // must give me: 60
}
答案 0 :(得分:0)
一点点类型的滥用会让你想要:
#include <vector>
#include <type_traits>
#include <cassert>
#include <iostream>
struct special_vector;
struct star
{
star(special_vector& v) : vec(std::addressof(v)) {}
special_vector* vec;
};
struct special_vector : std::vector<double>
{
using std::vector<double>::vector;
auto operator*() -> star { return star(*this);}
};
auto operator*(special_vector const& l, star const& r) -> double
{
assert(l.size() == r.vec->size());
double result = 0.0;
auto fl = l.cbegin(), fr = r.vec->cbegin(), ll = l.cend();
while (fl != ll)
{
result += (*fl++ * *fr++);
}
return result;
}
int main()
{
auto v1 = special_vector{ 1, 2, 3 };
auto v2 = special_vector{ 1, 2, 3 };
auto ans = v1 ** v2;
std::cout << ans << std::endl;
}
预期产出:
14
答案 1 :(得分:0)
我能够提出的解决方案非常快速且易于理解:
regex = r'\b\w+\b'
list=re.findall(regex,s)
list1='fruit.' + list
这允许您将数据存储在B类中,然后根据您的需要使用它。