[英]adding an operator to std::vector<T>
我正在嘗試向std :: vector添加一個運算符,以識別2個向量何時大致相同。 我該怎么做?
template<typename T> //only numeric types
class ImperciseVector: public std::vector<T> {
public:
ImperciseVector() {} //is this constructor needed?
bool operator~ (const ImperciseVector& v) {
T l1sq=0.0, l2sq=0.0, l3sq=0.0;
if ((*this).size() != v.size() || (*this).size==0 || v.size()==0) return false;
for (int j = 0; j<(*this).size(); j++) {
l1sq += (*this)[j]*(*this)[j];
l2sq += v[j]*v[j];
l3sq+= ((*this)[j]-v[j])*((*this)[j]-v[j]);
}
//some estimate such that length of their their difference (relative to their size) is small enough
if (l3sq/(l1sq*l2sq) <= 0.00001) return true;
return false;
}
};
當我向操作員編寫內容時,它不起作用,甚至無法識別。 如何正確或更好地做到這一點?
operator~()
是一元運算符。 它的使用只能是~iv
,不iv1 ~ iv2
。 在這種情況下,最好只編寫一個稱為諸如approx()
或重載operator==
類的成員函數(在某種意義上,對嗎?對,如果兩個ImpreciseVector
大致相等,則它們相等)
旁注,不要有ImpreciseVector<T>
從vector<T>
繼承。 請改用合成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.