簡體   English   中英

數學向量類通過組合

[英]mathematical vector class through composition

我開始寫一個類,它的行為很像std::vector但是有一些數學運算。 主要是矢量范數和重載重要數學運算符(+,-等)之類的東西,它們將按元素進行加,減。

該類發布在下面,我使用boost::operators編寫所有數學運算符,並且它們都可以完美地工作。 在實現迭代器時,我遇到了一些問題。 我試圖將迭代器編寫為嵌套類,並使用boost::iterator來獲取std::vector迭代器的大部分/全部功能。

這是我遇到麻煩的地方,代碼將無法使用大約2英里的模板相關錯誤輸出進行編譯。 如果您有興趣,但可以發布,但它是典型的冗長的Boost模板錯誤。

我的問題有兩個方面。

首先,構圖是最佳的設計選擇嗎? 使用私有繼承或裝飾器模式可能會做得更好嗎? 或者,也許有人知道在圖書館中實現這種想法的方法?

其次,迭代器在做什么? 我覺得好像在我的boost::iterator實現中缺少一些基本知識,並且想修復它而不是更改我的設計。

由於大多數方法微不足道或不重要,因此我並未將其包含在內。

//publicly inherits important boost::operators classes
template <class T>
class Coords: boost::arithmetic<Coords<T>
            ,boost::arithmetic<Coords<T>, T
//              ,boost::indexable<Coords<T>,int,T&
//              ,boost::dereferenceable<Coords<T>, T*>
//            >
    >
>
{
private:
    //private member
    std::vector<T> x_;
public:

    //public constructors
    Coords(int n, T x): x_(n,x){};
    Coords(int n): x_(n){};
    Coords(std::vector<T> &x);
    Coords(const Coords &rhs);

    //iterator nested class, public inherits boost::iterator_facade
    class iterator: public boost::iterator_facade<iterator, Coords<T>, std::random_access_iterator_tag>{
        private:
            typename std::vector<T>::iterator iter_;

            friend class boost::iterator_core_access;

            void increment() { iter_ = iter_++;};

            void decrement() { iter_ = iter_--;};

            void advance(int n){ iter_ = iter_+=n;};

            void equal(iterator const &other) const{
                return this->iter_ == other.iter_;
            }

            T& dereference() const {return *iter_;};

            int distance_to(iterator const &other) const{
                return this->iter_ - other.iter_;
            }

        public:
            iterator():iter_(0){};

            explicit iterator(const typename Coords<T>::iterator& it):iter_(it.iter_){};

            explicit iterator(const typename std::vector<T>::iterator& it):iter_(it){};

    };

    //methods from std::vector I would like to 'copy'
    typename Coords<T>::iterator begin(){return iterator(x_.begin());};
    typename Coords<T>::iterator end(){return iterator(x_.end());};
    typename Coords<T>::iterator operator[](std::size_t n);
    std::size_t size(){return x.size()};

    //mathematical methods
    T norm() const;
    T square() const;
    T sum() const;
    T dotProd(const Coords &rhs);

    //important operator overloads
    Coords operator+=(const T &rhs);
    Coords operator-=(const T &rhs);
    Coords operator*=(const T &rhs);
    Coords operator/=(const T &rhs);
    Coords operator+=(const Coords<T> &rhs);
    Coords operator-=(const Coords<T> &rhs);
    Coords operator*=(const Coords<T> &rhs);
    Coords operator/=(const Coords<T> &rhs);
};

這解決了該程序的許多問題:

#include <boost/operators.hpp>
#include <boost/iterator.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <vector>
//publicly inherits important boost::operators classes
template <class T>
class Coords: boost::arithmetic<Coords<T>
            ,boost::arithmetic<Coords<T>, T
//              ,boost::indexable<Coords<T>,int,T&
//              ,boost::dereferenceable<Coords<T>, T*>
//            >
    >
>
{
private:
    //private member
    std::vector<T> x_;
public:

    //public constructors
    Coords(int n, T x): x_(n,x){};
    Coords(int n): x_(n){};
    Coords(std::vector<T> &x);
    Coords(const Coords &rhs);

    //iterator nested class, public inherits boost::iterator_facade
    class iterator: public boost::iterator_facade<iterator, T, boost::random_access_traversal_tag >{
        private:
            typename std::vector<T>::iterator iter_;

            friend class boost::iterator_core_access;

            void increment() { ++iter_;}

            void decrement() { --iter_; }

            void advance(int n){ iter_+=n;};

            bool equal(iterator const &other) const{
                return this->iter_ == other.iter_;
            }

            T& dereference() const {return *iter_;};

            int distance_to(iterator const &other) const{
                return this->iter_ - other.iter_;
            }

        public:
            iterator():iter_(0){};

            iterator(const iterator& it):iter_(it.iter_){};
            iterator(iterator& it):iter_(it.iter_){};

            explicit iterator(const typename std::vector<T>::iterator& it):iter_(it){};

    };

    //methods from std::vector I would like to 'copy'
    iterator begin(){return iterator(x_.begin());};
    iterator end(){return iterator(x_.end());};
    iterator operator[](std::size_t n);
    std::size_t size(){return x_.size();};

    //mathematical methods
    T norm() const;
    T square() const;
    T sum() const;
    T dotProd(const Coords &rhs);

    //important operator overloads
    Coords operator+=(const T &rhs);
    Coords operator-=(const T &rhs);
    Coords operator*=(const T &rhs);
    Coords operator/=(const T &rhs);
    Coords operator+=(const Coords<T> &rhs);
    Coords operator-=(const Coords<T> &rhs);
    Coords operator*=(const Coords<T> &rhs);
    Coords operator/=(const Coords<T> &rhs);
};

int main() {
  Coords<int> c(3);
  for(Coords<int>::iterator it(c.begin()); it != c.end(); ++it)
    *it;
}
  • Boost文檔似乎說iterator_facade的第三個模板參數是boost::標簽,而不是std::標簽。
  • iterator_facade的第二個模板參數是值類型,而不是容器類型。
  • 用於incrementdecrementadvance所有產生的(我認為 )未定義行為的代碼。
  • 從類定義內部引用類成員時,無需列出類名。 在某些地方,必須刪除Coords<T>::

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM