c++ - 从函数返回 std::array 作为范围
问题描述
假设我有两个数组:
constexpr std::array<int, 3> a1{1, 2, 3};
constexpr std::array<int, 5> a2{1, 2, 3, 4, 5};
将它们转换为相同类型的范围以使其可以process_result
使用其返回值调用函数的正确方法是什么?
constexpr void process_result(RangeType range) { for (auto elem: range) { //do something with elem }
所以问题是什么RangeType
。
一个明显的解决方案是替换std::array
为std::vector
,但我想知道该怎么做std::array
。
解决方案
您可以使用 (non owning)std::span
来允许任何连续的范围:
constexpr void process_result(std::span<const int> range)
{
for (auto elem: range) {
//do something with elem
}
}
推荐阅读
- linear-regression - 拟合回归后如何使用Seaborn的residplot?
- ios - Swift,Firebase - 将 CMSampleBufferRef 与相机的实时供稿一起使用
- python - 使用 pandas 或 csr_matrix 工作的项-项矩阵稀疏数据
- powerbi - Power Bi 刷新中的触发方式选项?
- javascript - v-if 有两个条件
- android - ViewPager 未显示在 NestedScrollView 内,其中包含 ViewPager 下面的 RecyclerView
- node.js - 节点服务器如何自动重定向与浏览器相同
- php - 删除 gVIM 中的大于 (>) 字符突出显示,它是一个模板文件,而不是 HTML
- sql - T-SQL 中的指数微分
- python - 如何修复 Python(Django Rest 框架模板)中的 UnicodeDecodeError?