2
我正试图对我设计的库做一个防水接口。用户需要输入二维数据,因此我认为类似于std::transform
的迭代器界面将是透明的。 但是,我不确定如何异常处理滥用迭代器。异常处理迭代器接口
我的界面中,就像这样(我可以改变界面,如果有更好的一个):
template<typename InputItrX, typename InputItrY>
set_data(InputItrX beginX, InputItrX endX, InputItrY beginY)
{
//What exception handling should I do here?
size_t array_size = endX-beginX; //get the size of the xarray.
my_xVector.resize(array_size); //resize my internal container
my_yVector.resize(array_size); // ..and for the ydata.
std::copy(beginX, endX, my_xVector.begin()); //copy X
std::copy(beginY, beginY+array_size, my_yVector.begin()); //copy Y
}
例如,我的计划变得不确定,如果用户得到糊涂了接口,并写入
set_data(xdata.begin(), ydata.begin(), xdata.end());
或可能它们的xdata
有20个元素,但它们的ydata
没有。
是否有可能检查我的图书馆界面中的这种错误?
+1,我没有想过随机存取限制。 – Tom
为了完整性:随机访问迭代器检查在这里解决:http://stackoverflow.com/q/4307271/498253 – Tom