在C ++中,如果必须在编译时确定大小,则std :: array有什么意义? [英] In C++ what is the point of std::array if the size has to be determined at compile time?

查看:132
本文介绍了在C ++中,如果必须在编译时确定大小,则std :: array有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我觉得 std :: array 打算替代常规数组的STL。但是因为必须将数组大小作为模板参数传递,所以它阻止我们创建仅在运行时知道大小的 std :: array

Pardon my ignorance, it appears to me that std::array is meant to be an STL replacement for your regular arrays. But because the array size has to be passed as a template parameter, it prevents us from creating std::array with a size known only at runtime.

std::array<char,3> nums {1,2,3}; // Works.

constexpr size_t size = 3;
std::array<char,size> nums {1,2,3}; // Works.

const buf_size = GetSize();
std::array<char, buf_size> nums; // Doesn't work.

我认为C ++中数组的一个非常重要的用例是创建固定大小的数据基于运行时输入的结构(例如分配用于读取文件的缓冲区)。

I would assume that one very important use case for an array in C++ is to create a fixed size data structure based on runtime inputs (say allocating buffer for reading files).

我为此使用的解决方法是:

The workarounds I use for this are:

// Create a array pointer for on-the-spot usecases like reading from a file.
char *data = new char[size];
...
delete[] data;

或:

// Use unique_ptr as a class member and I don't want to manage the memory myself.
std::unique_ptr<char[]> myarr_ = std::unique_ptr<char[]>(new char[size]);

如果我不在乎固定大小,我知道我可以使用 std :: vector< char> ,其大小如下预定义:

If I don't care about fixed size, I am aware that I can use std::vector<char> with the size pre-defined as follows:

std::vector<char> my_buf (buf_size);

std :: array 选择忽略此用例?也许我不理解 std :: array 的真实用例。

Why did the designers of std::array choose to ignore this use case? Perhaps I don't understand the real usecase for std::array.

编辑:我想另一种方式我的问题也可能是这个短语-设计师为什么选择将尺寸作为模板参数而不是构造函数参数传递?选择后者会很难提供 std :: array 当前具有的功能吗?对我来说,这似乎是一个故意的设计选择,我不明白为什么。

I guess another way to phrase my question could also be - Why did the designers choose to have the size passed as a template param and not as a constructor param? Would opting for the latter have made it difficult to provide the functionality that std::array currently has? To me it seems like a deliberate design choice and I don't understand why.

推荐答案

易于编程


std :: array 促进了几个有益的接口和习惯用法,这些接口和习惯用法在 std :: vector 中使用。对于普通的C样式数组,不能有 .size()(没有 sizeof hack), .at()(超出范围的例外), front()/ back(),迭代器等。

Ease of programming

std::array facilitates several beneficial interfaces and idioms which are used in std::vector. With normal C-style arrays, one cannot have .size() (no sizeof hack), .at() (exception for out of range), front()/back(), iterators, so on. Everything has to be hand-coded.

许多程序员甚至可以为编译时已知的大小数组选择 std :: vector ,这仅仅是因为他们想利用上述编程方法。但这抢走了编译时固定大小数组的可用性能。

因此,库制造商提供了 std :: array 来阻止C语言的使用。样式的数组,但避免在编译时知道大小的 std :: vector s。

Many programmers may choose std::vector even for compile time known sized arrays, just because they want to utilize above programming methodologies. But that snatches away the performance available with compile time fixed size arrays.
Hence std::array was provided by the library makers to discourage the C-style arrays, and yet avoid std::vectors when the size is known at the compile time.

这篇关于在C ++中,如果必须在编译时确定大小,则std :: array有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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