c++ - 您如何使用标准跨度进行边界检查?
解决方案
相当笨重,但像这样:
- 使用位置
template<class Container>
auto& at(Container&& c, std::size_t pos){
if(pos >= c.size())
throw std::out_of_range("out of bounds");
return c[pos];
}
- 使用迭代器:
template<class Iterator, class Container>
auto& at(Container&& c, Iterator&& it){
if(std::distance(c.begin(), it) >= c.size())
throw std::out_of_range("out of bounds");
return *it;
}
推荐阅读
- git - git 在获取时会覆盖 refs/notes?
- matlab - 从图像中去除镜面反射
- php - drupal 7 - 获取介于 2 日期之间的网络表单提交
- python-3.x - 使用 for 循环嵌套字典
- acumatica - 我可以在销售订单屏幕中获取客户的自定义文件吗?
- c++ - 如何使用 QTextCursor 设置水平线样式
- php - 使用 code-igniter 从多个表中计数并获取最高记录
- c++ - Visual C++ 2013 二进制文件是否与 Visual C++ 2017 二进制文件兼容?
- javascript - 如何创建用作过滤器的段落
- asp.net - 检索 HubConnection 的状态