简体   繁体   English

typedef一个向量,并将boost :: numeric :: ublas :: vector固定大小

[英]typedef a vector and boost::numeric::ublas::vector of fixed size

I mean to typedef a name for a vector / boost vector with fixed size, and then for corresponding iterators. 我的意思是为具有固定大小的向量/ boost向量typedef一个名称,然后为对应的迭代器命名。 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 问题1:

  1. std::vector<double>

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

If it is not possible: 如果不可能:

  1. Question 2: What is an alternative implementation? 问题2:什么是替代实现? (other than that below). (以下内容除外)。

  2. Question 3: How would I typedef the iterators? 问题3:如何对迭代器进行typedef

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 (eg, this ). 但这带来了(至少)必须重新定义我自己的beginend和迭代器(例如this )的负担。 I mean to avoid this. 我的意思是避免这种情况。

Question 4: I put together two alternative lines in the definition of operator+= (see below). 问题4:我在operator+=的定义中放了两条替代行(请参见下文)。 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::vector nor (at the time of writing) boost::numeric::ublas::vector are designed to be a fixed size. std::vector和(在撰写本文时) boost::numeric::ublas::vector都不设计为固定大小。 There's no template parameter that specifies the size of the container. 没有模板参数指定容器的大小。

So no, a fixed sized typedef makes no sense for these containers. 因此,不行,固定大小的typedef对这些容器没有意义。

If you want to constrain the size of a std::vector , then one approach would be to write your own template class with a std::vector to model the payload. 如果要限制std::vector的大小,则一种方法是使用std::vector编写自己的模板类以对有效负载进行建模。

boost::numeric::ublas has a fixed size storage container and a fixed size vector. boost :: numeric :: ublas具有固定大小的存储容器和固定大小的向量。 I am not sure if you have to use typedef for iterator types and vector types. 我不确定是否必须将typedef用于迭代器类型和向量类型。 Here is a small example how to use both types and mix them even with std::array or std::vector . 这是一个小示例,说明如何同时使用两种类型以及将其与std::arraystd::vector混合使用。

#include <boost/numeric/ublas/storage.hpp>
#include <boost/numeric/ublas/vector.hpp>
#include <array>
#include <numeric>

using namespace boost::numeric;
using value_t = float;

int main () {
    constexpr auto N = 5ul;
    auto a = ublas::bounded_array<value_t,N>{N};
    auto b = ublas::fixed_vector<value_t,N>{};
    auto c = std::array<value_t,N>{};

    auto print = [](auto n, auto v){
        std::cout << n << "= "; 
        std::copy(v.begin(), v.end(), std::ostream_iterator<value_t>(std::cout, " ")); 
        std::cout << std::endl;
    };

    std::iota(a.begin(), a.end(), value_t(1));
    std::iota(b.begin(), b.end(), value_t(1));
    std::transform(a.begin(), a.end(), b.begin(), c.begin(), [](auto const& aa, auto const& bb){return aa+bb;});

    auto d = 2*b + b;

    print("a",a);
    print("b",b);
    print("c",c);
    print("d",d);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM