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

问题描述

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

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


  1. T [N] 固定大小的内置数组:N个T类型的连续元素;
    no size()或其他成员函数

  2. array< T,N> 一个T类型的N个连续元素的固定大小的数组;
    就像内置数组一样,但是可以解决大多数问题

  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;之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 08:45