c++ - C++中二维数组的标准实现

标签 c++ boost std

我需要存储一些二维数据数组,尺寸是固定的并且在运行时已知。是否有 array<T> 的二维等价物模板(在 std 库或 boost 中)?

最佳答案

谷歌:Boost.MultiArray

#include "boost/multi_array.hpp"
#include <cassert>

int 
main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  // Verify values
  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

关于c++ - C++中二维数组的标准实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639413/

相关文章:

c++ - Boost Dijkstra 代码不起作用?

c++ - 转换和验证字符串

c++ - 我应该对(数组索引)范围使用什么类型?

c++ - vtable 的编译时检测

c++ - std::ranges 如何与initializer_list 一起使用?

c++ - 当主线程显然未被阻塞时,QProgressDialog卡住

c++ - const unsigned char * 到 std::string

c++ - std::vector 的最大尺寸

c++ - 如何将 Windows GUID 转换为 boost::uuid?

c++ - boost 分词器/字符分隔符