C++ Boost.MultiArray任意开始和结束索引

C++ Boost.MultiArray任意开始和结束索引,c++,boost,C++,Boost,我有一节课看起来像这样。其目的是具有任意的起点和终点,但运算符[]映射到下界的索引0 template <class T> class Vec : public std::vector<T> { public: Vec() { this->reserve(32000); } Vec(std::string s, int upperbound, int lowerbound) { SetBoun

我有一节课看起来像这样。其目的是具有任意的起点和终点,但运算符[]映射到下界的索引0

template <class T>
class Vec : public std::vector<T>
{
public:
    Vec()
    {
        this->reserve(32000);
    }

    Vec(std::string s, int upperbound, int lowerbound)
    {
        SetBounds(s, upperbound, lowerbound);
    }

    void SetBounds(const std::string& s, int upperbound, int lowerbound)
    {
        mys = s;
        ub = upperbound;
        lb = lowerbound;

        std::cout << "resizing Vec for symbol: "
                  << symbol     << " "
                  << upperbound << " "
                  << lowerbound << " "
                  << upperbound - lowerbound << '\n';


        try
        {
           this->resize(ub - lb + 1);
        }
        catch(std::exception& ex)
        {
            std::cout << "Resize Exception: " << ex.what() << "\n";
        }
        catch(...)
        {
            std::cout << "SetBounds exception" << "\n";
        }
    }
public:
    T& operator[] (int idx)
    {
        try
        {
            //std::cout << idx << std::endl;

            return this->at(idx - lb);
        }
        catch(std::exception& ex)
        {
           std::cout << "Access Exception: " << idx << " " << symbol << " " << ex.what() << '\n';
        }
        catch(...)
        {
            std::cout << "Access Exception: " << idx << " "  << symbol << '\n';
        }
    }

private:
    std::string mys;
    int ub;
    int lb;
};
模板
类Vec:public std::vector
{
公众:
Vec()
{
这个->储备(32000);
}
Vec(标准::字符串s,整数上界,整数下界)
{
后退(s,上界,下界);
}
void SetBounds(const std::string&s,int上界,int下界)
{
mys=s;
ub=上限;
lb=下边界;
std::cout是

设置阵列基 在某些情况下,使用基于零的数组可能不方便或不方便。Boost.MultiArray组件提供了两种更改数组基的功能。一种可以使用extent_range类型将一对范围值指定给extent_gen构造函数,以设置基值

范例

typedef boost::multi_array<double, 3> array_type;
typedef boost::multi_array_types::extent_range range;
// OR typedef array_type::extent_range range;

array_type::extent_gen extents;

// dimension 0: 0-based
// dimension 1: 1-based
// dimension 2: -1 - based
array_type A(extents[2][range(1,4)][range(-1,3)]);
typedef boost::multi_array_type;
typedef boost::多数组类型::范围;
//或typedef数组\ u类型::范围\范围;
数组\类型::扩展\生成扩展;
//维度0:基于0的维度
//维度1:1-based
//维度2:-1-基于
阵列_类型A(范围[2][range(1,4)][range(-1,3)]);
另一种方法是首先正常构造数组,然后重置基。要将所有基设置为相同的值,请使用reindex成员函数,向其传递一个新的索引值


在内部,它是作为速度的指针算法实现的吗?它只是作为算法实现的。不是为了速度。而是为了实现(否则它将如何工作)。如果你的意思是这样的话,他们确实非常小心地让编译器对索引计算进行优化。这在英特尔CPU上非常重要,因为各种索引寻址模式可以与“手动”计算产生巨大的差异