C++ C++;代码改进,数组越界
这是数组的类模板。我重载了C++ C++;代码改进,数组越界,c++,arrays,class,operator-overloading,C++,Arrays,Class,Operator Overloading,这是数组的类模板。我重载了[]操作符,希望它能解决“越界”问题。打印输出工作正常,除非超出范围,否则编译器默认启用范围并显示6位数字 也许正在寻找一种更好的方法,用适当的元素号初始化数组,以便更好地检查,如果在查找元素时它确实超出范围,则显示一个错误 // implement the class myArray that solves the array index // "out of bounds" problem. #include <iostream> #include
[]
操作符,希望它能解决“越界”问题。打印输出工作正常,除非超出范围,否则编译器默认启用范围并显示6位数字
也许正在寻找一种更好的方法,用适当的元素号初始化数组,以便更好地检查,如果在查找元素时它确实超出范围,则显示一个错误
// implement the class myArray that solves the array index
// "out of bounds" problem.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
template <class T>
class myArray
{
private:
T* array;
int begin;
int end;
int size;
public:
myArray(int);
myArray(int, int);
~myArray() { };
void printResults();
// attempting to overload the [ ] operator to find correct elements.
int operator[] (int position)
{if (position < 0)
return array[position + abs(begin)];
else
return array[position - begin];
}
};
template <class T>
myArray<T>::myArray(int newSize)
{
size = newSize;
end = newSize-1;
begin = 0;
array = new T[size] {0};
}
template <class T>
myArray<T>::myArray(int newBegin, int newEnd)
{
begin = newBegin;
end = newEnd;
size = ((end - begin)+1);
array = new T[size] {0};
}
// used for checking purposes.
template <class T>
void myArray<T>::printResults()
{
cout << "Your Array is " << size << " elements long" << endl;
cout << "It begins at element " << begin << ", and ends at element " << end << endl;
cout << endl;
}
int main()
{
int begin;
int end;
myArray<int> list(5);
myArray<int> myList(2, 13);
myArray<int> yourList(-5, 9);
list.printResults();
myList.printResults();
yourList.printResults();
cout << list[0] << endl;
cout << myList[2] << endl;
cout << yourList[9] << endl;
return 0;
}
//实现解决数组索引的myArray类
//“越界”问题。
#包括
#包括
#包括
使用名称空间std;
模板
类myArray
{
私人:
T*阵列;
int开始;
内端;
整数大小;
公众:
myArray(int);
myArray(int,int);
~myArray(){};
作废打印结果();
//试图重载[]运算符以查找正确的元素。
整数运算符[](整数位置)
{if(位置<0)
返回数组[位置+abs(开始)];
其他的
返回数组[位置-开始];
}
};
模板
myArray::myArray(int newSize)
{
大小=新闻大小;
end=newSize-1;
开始=0;
数组=新的T[size]{0};
}
模板
myArray::myArray(intNewBegin,intNewEnd)
{
开始=新开始;
结束=新结束;
大小=((结束-开始)+1);
数组=新的T[size]{0};
}
//用于检查目的。
模板
void myArray::printResults()
{
cout首先,您的运算符[]
不正确。它被定义为总是返回int
。当您实例化某个数组时,您将得到编译时错误,该数组不能隐式转换为int
它应该是:
T& operator[] (int position)
{
//...
}
当然,还有:
const T& operator[] (int position) const
{
//you may want to also access arrays declared as const, don't you?
}
现在:
我重载了[]操作符,希望它能解决“越界”问题
您没有修复任何问题。您只允许阵列的客户端定义自定义边界,仅此而已。请考虑:
myArray<int> yourList(-5, 9);
yourList[88] = 0;
注意,在这种情况下抛出异常通常是最好的解决方案。引用std::out_of_range
doc:
这是程序可以抛出的标准异常。标准库的某些组件,如向量
、deque
、字符串
和位集
也会抛出此类异常,以表示参数超出范围
首先,您的运算符[]
不正确。它被定义为总是返回int
。当您实例化某个数组时,您将得到编译时错误,该数组不能隐式转换为int
它应该是:
T& operator[] (int position)
{
//...
}
当然,还有:
const T& operator[] (int position) const
{
//you may want to also access arrays declared as const, don't you?
}
现在:
我重载了[]操作符,希望它能解决“越界”问题
您没有修复任何问题。您只允许阵列的客户端定义自定义边界,仅此而已。请考虑:
myArray<int> yourList(-5, 9);
yourList[88] = 0;
注意,在这种情况下抛出异常通常是最好的解决方案。引用std::out_of_range
doc:
这是程序可以抛出的标准异常。标准库的某些组件,如向量
、deque
、字符串
和位集
也会抛出此类异常,以表示参数超出范围
首先,您的运算符[]
不正确。它被定义为总是返回int
。当您实例化某个数组时,您将得到编译时错误,该数组不能隐式转换为int
它应该是:
T& operator[] (int position)
{
//...
}
当然,还有:
const T& operator[] (int position) const
{
//you may want to also access arrays declared as const, don't you?
}
现在:
我重载了[]操作符,希望它能解决“越界”问题
您没有修复任何问题。您只允许阵列的客户端定义自定义边界,仅此而已。请考虑:
myArray<int> yourList(-5, 9);
yourList[88] = 0;
注意,在这种情况下抛出异常通常是最好的解决方案。引用std::out_of_range
doc:
这是程序可以抛出的标准异常。标准库的某些组件,如向量
、deque
、字符串
和位集
也会抛出此类异常,以表示参数超出范围
首先,您的运算符[]
不正确。它被定义为总是返回int
。当您实例化某个数组时,您将得到编译时错误,该数组不能隐式转换为int
它应该是:
T& operator[] (int position)
{
//...
}
当然,还有:
const T& operator[] (int position) const
{
//you may want to also access arrays declared as const, don't you?
}
现在:
我重载了[]操作符,希望它能解决“越界”问题
您没有修复任何问题。您只允许阵列的客户端定义自定义边界,仅此而已。请考虑:
myArray<int> yourList(-5, 9);
yourList[88] = 0;
注意,在这种情况下抛出异常通常是最好的解决方案。引用std::out_of_range
doc:
这是程序可以抛出的标准异常。标准库的某些组件,如向量
、deque
、字符串
和位集
也会抛出此类异常,以表示参数超出范围
重新定义数组类的更好选择是使用std库中的容器。and(由c++11支持)。它们都有重载运算符[],因此您可以访问数据。但是使用push_back添加元素(对于向量)方法,并使用at方法访问它们,消除了发生超出范围错误的机会,因为at方法执行检查并在需要时回推调整向量的大小。重新定义数组类的更好方法是使用std库中的容器。and(由c++11支持)。它们都有重载运算符[]因此您可以访问数据。但是使用push_-back(对于vector)方法添加元素并使用at方法访问它们可以消除超出范围e的机会