I mean to typedef a name for a vector / boost vector with fixed size, and then for corresponding iterators.
What I could do is (see below)
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;
The idea is to use later in my code something like
point_3d_array p;
for ( point_3d_const_iterator it = p.begin() ; it != p.end() ; it++ ) {
my code
}
Question 1: Is this possible with
std::vector<double>boost::numeric::ublas::vector<double>?
If it is not possible:
Question 2: What is an alternative implementation? (other than that below).
Question 3: How would I
typedefthe iterators?
As of now, since I couldn't find a way to do it, I defined my own class (see below).
But this carries the burden of (at least) having to redefine my own begin, end and iterators (e.g., this). I mean to avoid this.
Question 4:
I put together two alternative lines in the definition of operator+= (see below).
One of them is not working. What is the problem?
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;
};
Neither
std::vectornor (at the time of writing)boost::numeric::ublas::vectorare designed to be a fixed size. There's no template parameter that specifies the size of the container.So no, a fixed sized
typedefmakes no sense for these containers.If you want to constrain the size of a
std::vector, then one approach would be to write your own template class with astd::vectorto model the payload.