Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 任何人都知道一个模仿Pascal的类';s";范围数组";?_C++ - Fatal编程技术网

C++ 任何人都知道一个模仿Pascal的类';s";范围数组";?

C++ 任何人都知道一个模仿Pascal的类';s";范围数组";?,c++,C++,我不记得确切的名称-我几乎不记得关于Pascal的很多内容-但它只是一个数组,它进行了一些边界检查,因此您可以定义和使用如下内容: char arr[20..40]; 如果访问元素15或60,它将抛出一个越界异常 在C++中,我假设它看起来更像 vector<char> arr(20,40); 及 我在C++11数组上还没有跟上速度,所以可能它们已经在做类似的事情了?将执行std::vector::at,如果使用了无效的位置,将抛出类型为std::out_of_range的异常运

我不记得确切的名称-我几乎不记得关于Pascal的很多内容-但它只是一个数组,它进行了一些边界检查,因此您可以定义和使用如下内容:

char arr[20..40];
如果访问元素15或60,它将抛出一个越界异常

在C++中,我假设它看起来更像

vector<char> arr(20,40);


我在C++11数组上还没有跟上速度,所以可能它们已经在做类似的事情了?

将执行std::vector::at,如果使用了无效的位置,将抛出类型为
std::out_of_range
的异常<代码>运算符[]不执行边界检查


在您的示例中,
向量arr(20,40)
将构造一个值为40的20个整数的向量。这被称为重复序列构造函数。

C++数组不支持Pascal那样的自定义边界。它们总是从索引0开始,以索引长度-1结束。如果您想要类似Pascal的索引,您必须自己实现它,例如:

template<typename T, const int LowBound, const int HighBound>
class RangedArray
{
private:
    T m_arr[HighBound-LowBound+1];

    void CheckBounds(const int index)
    {
        if ((index < LowBound) || (index > HighBound))
            throw std::out_of_range();
    }

public:
    int low() const { return LowBound; }
    int high() const { return HighBound; }

    T operator[](const int index) const
    {
        CheckBounds(index);
        return m_arr[index-LowBound];
    }

    T& operator[](const int index)
    {
        CheckBounds(index);
        return m_arr[index-LowBound];
    }
};
模板
类范围Darray
{
私人:
T m_arr[上限下限+1];
无效检查边界(常量整数索引)
{
if((索引<下限)| |(索引>上限))
抛出标准::超出范围();
}
公众:
int low()常量{return LowBound;}
int high()常量{return HighBound;}
T运算符[](常量整数索引)常量
{
检查边界(索引);
返回m_arr[索引下限];
}
T运算符[](常量整数索引)
{
检查边界(索引);
返回m_arr[索引下限];
}
};

RangedArray-arr;
arr[20]//好的
arr[15]//超出范围
arr[60]//超出范围
如果您想要更具动态性的内容,请尝试以下方法:

template<typename T, const int LowBound>
class RangedVector
{
private:
    std::vector<T> m_vec;

    void CheckBounds(const int index)
    {
        if ((index < low()) || (index > high()))
            throw std::out_of_range();
    }

public:
    int low() const { return LowBound; }
    int high() const { return m_vec.empty() ? -1 : (LowBound + m_vec.size() - 1); }

    void setHighBound(const int HighBound)
    {
        if (HighBound < LowBound)
            throw something;
        m_vec.resize(HighBound-LowBound+1);
    }

    void push_back(const T &value)
    {
        m_vec.push_back(value);
    }

    T operator[](const int index) const
    {
        CheckBounds(index);
        return m_vec[index-LowBound];
    }

    T& operator[](const int index)
    {
        CheckBounds(index);
        return m_vec[index-LowBound];
    }
};
模板
类RangeVector
{
私人:
std::向量m_-vec;
无效检查边界(常量整数索引)
{
如果((索引<低())| |(索引>高())
抛出标准::超出范围();
}
公众:
int low()常量{return LowBound;}
int high()常量{return m_vec.empty()?-1:(LowBound+m_vec.size()-1);}
void setHighBound(常量int HighBound)
{
if(上限<下限)
扔东西;
m_向量调整大小(上限下限+1);
}
无效推回(常数T和值)
{
m_向量推回(值);
}
T运算符[](常量整数索引)常量
{
检查边界(索引);
返回m_vec[索引下限];
}
T运算符[](常量整数索引)
{
检查边界(索引);
返回m_vec[索引下限];
}
};

RangedVector arr;
arr.setHighBound(40);
arr[20]//好的
arr[15]//超出范围
arr[60]//超出范围

vector
at()
已经进行了范围检查,因此它将非常简单(类似于
返回数据。at(索引较低);
)。我希望避免使用自己的方法,但这是一个非常好的起点。谢谢
template<typename T, const int LowBound, const int HighBound>
class RangedArray
{
private:
    T m_arr[HighBound-LowBound+1];

    void CheckBounds(const int index)
    {
        if ((index < LowBound) || (index > HighBound))
            throw std::out_of_range();
    }

public:
    int low() const { return LowBound; }
    int high() const { return HighBound; }

    T operator[](const int index) const
    {
        CheckBounds(index);
        return m_arr[index-LowBound];
    }

    T& operator[](const int index)
    {
        CheckBounds(index);
        return m_arr[index-LowBound];
    }
};
RangedArray<char, 20, 40> arr;
arr[20] // OK
arr[15] // out of bounds
arr[60] // out of bounds
template<typename T, const int LowBound>
class RangedVector
{
private:
    std::vector<T> m_vec;

    void CheckBounds(const int index)
    {
        if ((index < low()) || (index > high()))
            throw std::out_of_range();
    }

public:
    int low() const { return LowBound; }
    int high() const { return m_vec.empty() ? -1 : (LowBound + m_vec.size() - 1); }

    void setHighBound(const int HighBound)
    {
        if (HighBound < LowBound)
            throw something;
        m_vec.resize(HighBound-LowBound+1);
    }

    void push_back(const T &value)
    {
        m_vec.push_back(value);
    }

    T operator[](const int index) const
    {
        CheckBounds(index);
        return m_vec[index-LowBound];
    }

    T& operator[](const int index)
    {
        CheckBounds(index);
        return m_vec[index-LowBound];
    }
};
RangedVector<char, 20> arr;
arr.setHighBound(40);
arr[20] // OK
arr[15] // out of bounds
arr[60] // out of bounds