C ++ valarray与向量 [英] C++ valarray vs. vector

查看:220
本文介绍了C ++ valarray与向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢向量很多。他们漂亮,快速。但我知道这个东西叫一个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与向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆