T [N]与std :: array< T,N>之间的差异 [英] Difference between T[N] and std::array<T,N>

查看:74
本文介绍了T [N]与std :: array< T,N>之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单,但是更具体地说,我想引述 Stroustrup11 .

My question is pretty straightforward, but to be more specific I want to quote 2 lines from Stroustrup11.

  1. T [N] 一个固定大小的内置数组:N个T类型的连续元素;没有 size()或其他成员函数
  2. array< T,N> 一个固定大小的数组,由N个T类型的连续元素组成;类似于内置数组,但大多数问题已解决
  1. T[N] A fixed-size built-in array: N contiguous elements of type T; no size() or other member functions
  2. array<T,N> A fixed-size array of N contiguous elements of type T; like the built-in array, but with most problems solved

那么作者提到的有什么区别?以及 std :: array< T,N> 可以解决什么问题?

So what is the difference the author is mentioning? And what problems are solved for std::array<T,N> ?

推荐答案

主要区别在于 std :: array< T,N> 不会衰减到指向 T [N] 将位于的第一个元素的指针,,您可以获取 std :: array< T,N> .

The principal differences are that std::array<T, N> doesn't decay to a pointer to the first element where T[N] would, and you can take a value copy of a std::array<T, N>.

std :: array 还提供了一些有用的功能,例如字典比较运算符.

std::array also offers some useful functions, such as lexicographical comparison operators.

但是,由于 N 必须是可在编译时评估的常量表达式,因此 std :: vector< T> 通常是首选.

But because N has to be a compile time evaluable constant expression, std::vector<T> is often the preferred choice.

这篇关于T [N]与std :: array&lt; T,N&gt;之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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