我的意思是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:这可能吗
std::vector<double>
boost::numeric::ublas::vector<double>
?
如果不可能:
问题 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/