你能像现代Fortran一样处理C++中的数组部分吗?

huangapple go评论68阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月23日 17:59:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定