申报多维STD更简洁的方式::阵列
短的问题: 有一个较短的方式做到这一点申报多维STD更简洁的方式::阵列
array<array<atomic<int>,n>,m> matrix;
我希望这样的事情
array< atomic< int>,n,m> matrix;
,但它不工作。 ..
当嵌套时,性病::阵列可以变得非常难以阅读和不必要的冗长。尺寸的相反顺序可能会特别令人困惑。
例如:
std::array < std::array <int, 3 > , 5 > arr1;
相比
char c_arr [5][3];
另外,还要注意()开始,结束(),大小()都返回无意义的值,当你窝的std ::阵列。
基于这些原因,我创建了自己的固定大小的多维数组的容器,array_2d和array_3d。他们的优点是可以与C++ 98一起工作。
它们类似于std :: array,但对于2维和3维的多维数组。它们比内置多维阵列更安全,性能更差。我不包括多维数组的容器,其尺寸大于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
ü规则,我想知道他们为什么没有这样做在std ::,可能他们想保持它纯粹的通用:) – NoSenseEtAl
btw为什么是.size()毫无意义?它不像预期的那样? arr.size()== dim1,arr [0] .size()= dim2 – NoSenseEtAl
也许毫无意义的话太强大了。我的意思是它不返回多维数组中元素的总数。顺便说一句,我同意像它这样的东西应该在标准。会成为不安全的内置多维数组的一个很好的选择:) – Ricky65
模板别名可能助阵:
#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;
}
不幸的是,目前没有编译器实现这个功能(至少我知道,我很乐意被证明是错误的):( –
非常好,我很害怕MACRO黑魔法是唯一的方法(对HELL):P – NoSenseEtAl
我在发布之前在clang/OS X上测试过它 –
对于不支持模板别名尚未编译器可口的解决方法是使用一个简单的元函数生成的类型:
#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;
}
使用解决方案可变参数模板(略高于模板别名更加复杂,但更普遍目的)
template <typename T, std::size_t thisSize, std::size_t ... otherSizes>
class multi_array : private std::array<multi_array<T, otherSizes...>, thisSize>
{
using base_array = std::array<multi_array<T, otherSizes...>, thisSize>;
public:
using base_array::operator[];
// TODO: add more using statements to make methods
// visible. This is less typing (and less error-prone)
// than forwarding to the base_array type.
};
template <typename T, std::size_t thisSize>
class multi_array<T, thisSize> : private std::array<T, thisSize>
{
using base_array = std::array<T, thisSize>;
public:
using base_array::operator[];
// TODO: add more using statements to make methods
// visible. This is less typing (and less error-prone)
// than forwarding to the base_array type.
};
分配给数组的非叶子可能会有一些改进。
我有一个相对较新的构建铛/ LLVM的测试。
享受!
这里有一个简单,通用的版本:
template <typename T, size_t d1, size_t d2, size_t... ds>
struct GetMultiDimArray
{
using type = std::array<typename GetMultiDimArray<T, d2, ds...>::type, d1>;
};
template <typename T, size_t d1, size_t d2>
struct GetMultiDimArray<T, d1, d2>
{
using type = std::array<std::array<T, d2>, d1>;
};
template <typename T, size_t d1, size_t d2, size_t... ds>
using MultiDimArray = typename GetMultiDimArray<T, d1, d2, ds...>::type;
// Usage:
MultiDimArray<int, 3, 2> arr {1, 2, 3, 4, 5, 6};
assert(arr[1][1] == 4);
小心:像'std :: array :: size()'这样的方法不会返回正确的值。另外,像'std :: array :: fill(value_type)'这样的方法根本不起作用。 – Ethan
什么不起作用?什么是错误信息?这将有助于某人真正回答这个问题。我明白模板错误本身很冗长,但至少有一些会有所帮助。 – Mahesh
对于初学者,它缺少一个'>'。 – Mat
array test; 错误的模板参数(3,应该是2) –
NoSenseEtAl