C ++ 11的std ::阵VS静态数组VS的std ::矢量 [英] c++11 std::array vs static array vs std::vector

查看:170
本文介绍了C ++ 11的std ::阵VS静态数组VS的std ::矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题,是一件好事,开始使用C ++ 11,如果我将开发一个code为3年以下?

First question, is it a good thing to start using c++11 if I will develop a code for the 3 following years?

接着,如果是,什么是最好的方式来实现一个矩阵,如果我想与LAPACK使用它呢?我的意思是,做的std ::矢量<的std ::矢量<类型> >矩阵是不容易LAPACK兼容。

Then if it is, what is the "best" way to implement a matrix if I want to use it with Lapack? I mean, doing std::vector<std::vector< Type > > Matrix is not easily compatible with Lapack.

到现在为止,我存储我的矩阵键入*矩阵(新类型[N])(指针形式删除是很重要的,因为数组的大小并不像许多像5中给出,但作为一个变量)。

Up to now, I stored my matrix with Type* Matrix(new Type[N]) (the pointer form with new and delete were important because the size of the array is not given as a number like 5, but as a variable).

但随着C ++ 11,可以使用std ::数组。根据这一网站,这个容器似乎是最好的解决办法......做什么你觉得呢?

But with C++11 it is possible to use std::array. According to this site, this container seems to be the best solution... What do you think?

推荐答案

首先第一件事情,如果你要学习C ++,学习C ++ 11。在previous C ++标准是在2003年发布,这意味着它已经的 10 的岁。这是一个很大的IT世界。 C ++ 11的技能也将顺利转化为即将到来的C ++ 1Y(最可能是C ++ 14)标准。

First things first, if you are going to learn C++, learn C++11. The previous C++ standard was released in 2003, meaning it's already ten years old. That's a lot in IT world. C++11 skills will also smoothly translate to upcoming C++1y (most probably C++14) standard.

的std ::矢量的std ::阵列之间的主要区别是动态的(大小和分配)和静态存储器。所以,如果你想有一个矩阵类,总是,也就是说,4×4,的std ::阵列&LT;浮动,4×4方式&gt; 会做得很好。

The main difference between std::vector and std::array is the dynamic (in size and allocation) and static storage. So if you want to have a matrix class that's always, say, 4x4, std::array<float, 4*4> will do just fine.

这两个类提供了。数据()成员,它应该产生一个兼容的指针。但是请注意,的std ::矢量&lt;的std ::矢量&lt;浮动&GT;&GT; 将不occuppy连续 16 *的sizeof(浮动)内存(以便 v [0]。数据() 不会的工作)。如果你需要一个动态调整的矩阵,用单矢量,并将其调整为宽*高尺寸

Both of these classes provide .data() member, which should produce a compatible pointer. Note however, that std::vector<std::vector<float>> will NOT occuppy contiguous 16*sizeof(float) memory (so v[0].data() won't work). If you need a dynamically sized matrix, use single vector and resize it to the width*height size.

由于访问内容将是一个有点困难(​​ v [宽* Y + X] v [高* X + Y ] ),你可能想提供一个包装类,可以让你按行/列对访问任意字段。

Since the access to the elements will be a bit harder (v[width * y +x] or v[height * x + y]), you might want to provide a wrapper class that will allow you to access arbitrary field by row/column pair.

既然你也提到了C风格的数组; 的std ::阵列提供了更好的接口来处理同一类型的存储,因而应该是preferred;没有什么获得与静态数组结束了的std ::阵列

Since you've also mentioned C-style arrays; std::array provides nicer interface to deal with the same type of storage, and thus should be preferred; there's nothing to gain with static arrays over std::array.

这篇关于C ++ 11的std ::阵VS静态数组VS的std ::矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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