有没有办法指定嵌套 STL 向量 C++ 的维度?

发布于 2024-08-29 01:18:00 字数 227 浏览 2 评论 0原文

我知道向量可以构造为预定义的大小

vector<int> foo(4);

但是有没有办法指定嵌套向量的维度?

vector< vector<int> > bar(4);

假设我想要一个大小为 4 的向量,其中包含大小为 4 的向量...就像一个 4x4 多维整数数组?

I know vectors can be constructed to a predefined size

vector<int> foo(4);

But is there a way to specify the dimensions of nested vectors?

vector< vector<int> > bar(4);

Lets say I wanted a vector of size 4 containing vector's of size 4... like a 4x4 multidimensional array of ints?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-09-05 01:18:00

该构造函数的第二个参数是要初始化的值。现在您将获得 4 个默认构造的向量。用一个更简单的 1D 示例来澄清:

// 4 ints initialized to 0
vector<int> v1(4);

// *exactly* the same as above, this is what the compiler ends up generating
vector<int> v2(4, 0); 

// 4 ints initialized to 10
vector<int> v3(4, 10); 

所以您想要:

vector< vector<int> > bar(4, vector<int>(4));
//              this many ^   of these ^

这将创建一个由整数向量组成的向量,初始化为包含 4 个向量,这些向量被初始化为包含 4 个整数,初始化为 0。(您可以为 int 指定一个默认值,如果需要的话。)

满嘴,但不太硬。 :)


对于一对:

typedef std::pair<int, int> pair_type; // be liberal in your use of typedef
typedef std::vector<pair_type> inner_vec;
typedef std::vector<inner_vec> outer_vec;

outer_vec v(5, inner_vec(5, pair_type(1, 1)); // 5x5 of pairs equal to (1, 1)
//             this many ^ of these ^
//this many ^      of these ^

The second argument to that constructor is the value to initialize with. Right now you're getting 4 default-constructed vectors. To clarify with a simpler 1D example:

// 4 ints initialized to 0
vector<int> v1(4);

// *exactly* the same as above, this is what the compiler ends up generating
vector<int> v2(4, 0); 

// 4 ints initialized to 10
vector<int> v3(4, 10); 

So you want:

vector< vector<int> > bar(4, vector<int>(4));
//              this many ^   of these ^

This creates a vector of vectors of ints, initialized to contain 4 vectors that are initialized to contain 4 ints, initialized to 0. (You could specify a default value for the int to, if desired.)

A mouth-full, but not too hard. :)


For a pair:

typedef std::pair<int, int> pair_type; // be liberal in your use of typedef
typedef std::vector<pair_type> inner_vec;
typedef std::vector<inner_vec> outer_vec;

outer_vec v(5, inner_vec(5, pair_type(1, 1)); // 5x5 of pairs equal to (1, 1)
//             this many ^ of these ^
//this many ^      of these ^
神魇的王 2024-09-05 01:18:00

除了 std::vector 之外,您还可以使用 boost::multi_array。来自文档

#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;
}

Alternatively to a std::vector you can use boost::multi_array. From the documentation:

#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文