简短的问题:有没有更短的方法?
array<array<atomic<int>,n>,m> matrix;
我希望是这样的
array< atomic< int>,n,m> matrix;
但是它不工作..。
发布于 2011-10-29 21:11:21
嵌套时,std::array可能会变得非常难以读取,并且会变得不必要地冗长。维度的相反顺序可能特别令人困惑。
例如:
std::array < std::array <int, 3 > , 5 > arr1;
与
char c_arr [5][3];
还要注意,当嵌套std::array时,begin()、end()和size()都会返回无意义的值。
出于这些原因,我创建了自己的固定大小多维数组容器array_2d和array_3d。他们有一个优势,那就是与C++98合作。
它们类似于std::array,但用于二维和三维的多维数组。与内置的多维数组相比,它们更安全,性能也不会差。我没有包含维数大于3的多维数组的容器,因为它们并不常见。在C++11中,可以创建支持任意数量的维度的可变模板版本(类似于Michael Price的示例)。
二维变体的一个示例:
//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
发布于 2011-10-07 23:29:32
模板别名可能会有所帮助:
#include <array>
template <class T, unsigned I, unsigned J>
using Matrix = std::array<std::array<T, J>, I>;
int main()
{
Matrix<int, 3, 4> matrix;
}
发布于 2011-10-08 00:06:23
对于还不支持模板别名的编译器来说,一个令人满意的解决办法是使用一个简单的元函数来生成类型:
#include <cstddef>
#include <array>
template<class T, std::size_t RowsN, std::size_t ColumnsN>
struct Matrix
{
typedef std::array<std::array<T, ColumnsN>, RowsN> type; // row major
private:
Matrix(); // prevent accidental construction of the metafunction itself
};
int main()
{
Matrix<int, 3, 4>::type matrix;
}
https://stackoverflow.com/questions/7689288
复制相似问题