问题描述
我喜欢向量很多。他们漂亮,快速。但我知道这个东西叫一个valarray存在。为什么要使用valarray而不是向量?我知道valarrays有一些语法糖,但除此之外,它们何时有用?
I like vectors a lot. They're nifty and fast. But I know this thing called a valarray exists. Why would I use a valarray instead of a vector? I know valarrays have some syntactic sugar, but other than that, when are they useful?
推荐答案
Valarrays把Fortran的一些速度带到C ++。你不会做一个指针的valarray,所以编译器可以做出关于代码的假设和优化它更好。 (Fortran的这么快的主要原因是没有指针类型所以没有指针别名。)
Valarrays (value arrays) are intended to bring some of the speed of Fortran to C++. You wouldn't make a valarray of pointers so the compiler can make assumptions about the code and optimise it better. (The main reason that Fortran is so fast is that there is no pointer type so there can be no pointer aliasing.)
Valarrays也有类,允许你切片尽管该标准的一部分可以使用一些更多的工作。调整它们的大小是破坏性的,并且缺乏迭代器。
Valarrays also have classes which allow you to slice them up in a reasonably easy way although that part of the standard could use a bit more work. Resizing them is destructive and they lack iterators.
因此,如果它是你正在使用的数字,方便并不是所有重要的使用valarrays。否则,向量只是更方便。
So, if it's numbers you are working with and convenience isn't all that important use valarrays. Otherwise, vectors are just a lot more convenient.
这篇关于C ++ valarray与向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!