英文:
Can you handle array sections in C++ in a similar fashion as modern fortran?
问题
在C++中是否有类似的方法?在我的代码开头应该#include什么来具有类似的数组处理能力?
英文:
Let's say A is a 9x9 array. If I want to copy the contents of row 4 into the 1x9 array V, in fortran (90 or later) this can be done as
V = A(:,4)
One could also access elements 4 through 8 in V with
W = V(5:8)
or perhaps have a 2x5 subarray of A:
S = A(3:4, 5:9)
This is only a matter of accessing stored values so it causes very little overhead.
Isn't there something as straightforward in C++? What should I #include at the beginning of my code to have similar array handling capabilities?
答案1
得分: 2
有一个关于 std::submdspan
的提案,基于 std::mdspan
进行构建,希望能够包含在 C++23 中。如果我理解正确,这正是你所需要的:
subspan 或者“切片”函数,返回现有
mdspan
的子集视图
...
创建子视图是许多编程语言中多维数组的重要功能。这些语言包括 Fortran、Matlab、Python 和 Python 的 NumPy 扩展。
因此,在标准的 C++ 中目前还没有“非常直接”的方式,但这可能会很快出现。不过,你可以使用第三方库或者使用迭代器编写一个简化版本。
英文:
There is a proposal for std::submdspan
, building on std::mdspan
, hopefully to be included in C++23. If I undestand correctly, this is exactly what you would want:
>the subspan or “slicing” function that returns a view of a subset of an existing mdspan
> ...
> Creating subspans is an integral capability of many, if not all programming languages with multidimensional arrays. These include Fortran, Matlab, Python, and Python’s NumPy extension.
So there is nothing "as straightforward" in standard C++ yet, but it might come soon. However, you could use a third-party library or just write a simpler version of it using iterators.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论