c++ - typedef 一个 vector 和固定大小的 boost::numeric::ublas::vector

标签 c++ vector boost typedef boost-ublas

我的意思是typedef具有固定大小的 vector/boost vector 的名称,然后是相应的迭代器。 我可以做的是(见下文)

typedef std::array<double, 3> point_3d_array;
typedef point_3d_array::iterator point_3d_iterator;
typedef point_3d_array::const_iterator point_3d_const_iterator;

我的想法是稍后在我的代码中使用类似的东西

point_3d_array p;
for ( point_3d_const_iterator it = p.begin() ; it != p.end() ; it++ ) {
   my code
}

问题 1:这可能吗

  1. std::vector<double>

  2. boost::numeric::ublas::vector<double> ?

如果不可能:

  1. 问题 2:什么是替代实现? (以下除外)。

  2. 问题 3:我会怎样 typedef迭代器?

截至目前,由于我找不到实现它的方法,所以我定义了自己的类(见下文)。 但这带来了(至少)必须重新定义我自己的负担 begin , end和迭代器(例如 this )。我的意思是避免这种情况。

问题 4: 我在 operator+= 的定义中将两条替代行放在一起(见下文)。 其中一个不工作。有什么问题?

typedef std::array<double, 3> point_3d_array;
typedef point_3d_array::iterator point_3d_iterator;
typedef point_3d_array::const_iterator point_3d_const_iterator;

class point_3d {
public:
    /*
     * Default constructor
     */
    point_3d() : _point_3d({ 0, 0, 0 }) { };
    /*
     * Initialization constructor
     * Is copy constructor automatically generated?
     */
    point_3d(const double x1, const double x2, const double x3) : _point_3d({x1, x2, x3}) {};

    /*
     * Iterator members
     */
    point_3d_iterator begin() { return _point_3d.begin(); }
    point_3d_iterator end() { return _point_3d.end(); }
    point_3d_const_iterator begin() const { return _point_3d.begin(); }
    point_3d_const_iterator end() const { return _point_3d.end(); }
    /*
     * Array subscript operators
     */
    double & operator[](size_t i) {
        return this->_point_3d[ i ];
    }
    const double & operator[](size_t i) const {
        return this->_point_3d[ i ];
    }

    /*
     * Basic operation members
     */
    point_3d & operator+=(const point_3d &rhs) {
        for ( size_t i = 0 ; i < this->_point_3d.size() ; i++ ) {
            //this[ i ] += rhs[ i ];  // Why are Array subscript operators not working in the lhs?
            this->_point_3d[ i ] += rhs[ i ];
        }
        return *this;
    }

private:
    point_3d_array _point_3d;
};

最佳答案

std::vector 和(在撰写本文时)boost::numeric::ublas::vector 都不是设计是一个固定的大小。没有指定容器大小的模板参数。

所以不,固定大小的 typedef 对这些容器没有意义。

如果您想限制 std::vector 的大小,那么一种方法是编写您自己的带有 std::vector 的模板类来建模有效载荷。

关于c++ - typedef 一个 vector 和固定大小的 boost::numeric::ublas::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55121405/

相关文章:

c++ - 我怎样才能将原始数组的 memcpy 等效于 std::vector?

c++ - 为什么 Boost 参数选择继承而不是组合?

java - 在关闭远程桌面连接时运行 s/w

c++ - 在 SFML 中获取绝对顶点坐标?

C++ map 比较

c++组织变量进行计算 - 速度和可读性

c++ - Std 迭代器赋值错误

c++ - 如何删除 vector 中的元素。 (删除无效)

c++ - has::std::has_nothrow_default_constructor 被移动/改变了吗?

C# BlockingCollection 的 c++/boost 模拟?