问题描述
有没有preexisting库,可以让我创建一个具有以下属性类似数组的对象:
Is there a preexisting library that will let me create array-like objects which have the following properties:
- 运行时间尺寸规格(在instantition选择,没有长大以后或缩小)
- 符重载执行元素方式操作(即
C = A + B
将导致一个vectorC
与C [I] = A [I] + b [I]
所有I
,同样为*
,-
,/
等) - 一套好的它作用的elementwise,例如
X =开方(VEC)
将元素X [I] =开方(VEC功能[I])
- 提供总结的功能,例如
总和(VEC)
,平均值(VEC)
等 - (可选的)的操作可以被发送到用于处理的GPU。
- Run time size specification (chosen at instantition, not grown or shrunk afterwards)
- Operators overloaded to perform element wise operations (i.e.
c=a+b
will result in a vectorc
withc[i]=a[i]+b[i]
for alli
, and similarly for*
,-
,/
, etc) - A good set of functions which act elementwise, for example
x=sqrt(vec)
will have elementsx[i]=sqrt(vec[i])
- Provide "summarising" functions such as
sum(vec)
,mean(vec)
etc - (Optional) Operations can be sent to a GPU for processing.
基本上类似阵列Fortran中工作,与所有隐藏的执行方式。目前我使用矢量
从STL和手动重载运营商,但我觉得这可能是一个解决的问题。
Basically something like the way arrays work in Fortran, with all of the implementation hidden. Currently I am using vector
from the STL and manually overloading the operators, but I feel like this is probably a solved problem.
推荐答案
在标准库中,每个人都早已被人遗忘的角落尘土飞扬,坐在一类称为的valarray
。看看它,看看它是否适合你的需要。
In the dusty corners of standard library, long forgotten by everyone, sits a class called valarray
. Look it up and see if it suits your needs.
从CP preference.com 手册页:
From manual page at cppreference.com:
的std :: valarray的
重presenting和操作值数组类。它支持元素明智的数学运算和各种形式的推广下标运算符,切片和间接访问的。
A code snippet for illustration:
#include <valarray>
#include <algorithm>
#include <iterator>
#include <iostream>
int main()
{
std::valarray<int> a { 1, 2, 3, 4, 5};
std::valarray<int> b = a;
std::valarray<int> c = a + b;
std::copy(begin(c), end(c),
std::ostream_iterator<int>(std::cout, " "));
}
这篇关于用C元素智能操作++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!