声明多维 std::array 的不那么冗长的方法

less verbose way to declare multidimensional std::array(声明多维 std::array 的不那么冗长的方法)

本文介绍了声明多维 std::array 的不那么冗长的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短的问题:有没有更短的方法来做到这一点

Short question: Is there a shorter way to do this

array<array<atomic<int>,n>,m> matrix;

我希望像

array< atomic< int>,n,m> matrix;    

但它不起作用...

推荐答案

嵌套时,std::array 会变得非常难以阅读并且变得冗长.维度的相反顺序可能特别令人困惑.

When nested, std::array can become very hard to read and unnecessarily verbose. The opposite ordering of the dimensions can be especially confusing.

例如:

std::array < std::array <int, 3 > , 5 > arr1; 

对比

char c_arr [5][3]; 

另外,注意当你嵌套 std::array 时,begin()、end() 和 size() 都返回无意义的值.

Also, note that begin(), end() and size() all return meaningless values when you nest std::array.

出于这些原因,我创建了自己的固定大小的多维数组容器,array_2d 和 array_3d.他们的优势是可以使用 C++98.

For these reasons I've created my own fixed size multidimensional array containers, array_2d and array_3d. They have the advantage that they work with C++98.

它们类似于 std::array,但适用于 2 维和 3 维多维数组.它们比内置多维数组更安全,性能也不差.我没有包含维度大于 3 的多维数组的容器,因为它们不常见.在 C++11 中,可以制作支持任意维数的可变参数模板版本(类似于 Michael Price 的示例).

They are analogous to std::array but for multidimensional arrays of 2 and 3 dimensions. They are safer and have no worse performance than built-in multidimensional arrays. I didn't include a container for multidimensional arrays with dimensions greater than 3 as they are uncommon. In C++11 a variadic template version could be made which supports an arbitrary number of dimensions (Something like Michael Price's example).

二维变体的一个例子:

//Create an array 3 x 5 (Notice the extra pair of braces) 
fsma::array_2d <double, 3, 5> my2darr = {{ 
{ 32.19, 47.29, 31.99, 19.11, 11.19}, 
{ 11.29, 22.49, 33.47, 17.29, 5.01 }, 
{ 41.97, 22.09, 9.76, 22.55, 6.22 } 
}};  

此处提供完整文档:http://fsma.googlecode.com/files/fsma.html

您可以在此处下载库:http://fsma.googlecode.com/files/fsma.zip

这篇关于声明多维 std::array 的不那么冗长的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:声明多维 std::array 的不那么冗长的方法

基础教程推荐