简体   繁体   English

C++ 如何制作 std::list 数组?

[英]C++ How to make a std::list of arrays?

I have a class that contains a 2D array.我有一个包含二维数组的类。

int arr[3][3];

How would I go about storing that matrix in a std::list?我将如何将该矩阵存储在 std::list 中?

list<*> explored;

explored.pushback(classname->arr);

I thought maybe because I already knew the size of the arrays I would just create a list of pointers with something like the above but that obviously doesn't work.我想也许是因为我已经知道数组的大小,所以我只会创建一个具有类似上述内容的指针列表,但这显然不起作用。 How would I initialize the list?我将如何初始化列表? How would I go about accessing the 2D arrays individually?我将如何单独访问二维数组?

Edit: I wanted to have a list of multiple 2D arrays.编辑:我想要一个包含多个二维数组的列表。 Meaning each index position would hold an array.这意味着每个索引位置将保存一个数组。 In order to solve my problem I instead decided to make a class, have the class hold a matrix.为了解决我的问题,我决定创建一个类,让这个类拥有一个矩阵。 Then I would simply get the matrix by doing something like然后我会简单地通过做类似的事情来获得矩阵

Class Node{
    Int matrix[3][3];
}
//Store a node with a matrix inside of it.
list<node> v;
v.pushback(node);


//retrieve the matrix by iterating to that position in the list then
v.matrix;

How would I go about storing that matrix in a std::list?我将如何将该矩阵存储在 std::list 中?

You cannot store raw arrays in a std::list .您不能将原始数组存储在std::list

From https://en.cppreference.com/w/cpp/container/list ,https://en.cppreference.com/w/cpp/container/list

T must meet the requirements of CopyAssignable and CopyConstructible (until C++11). T必须满足CopyAssignableCopyConstructible的要求(直到 C++11)。

The requirements that are imposed on the elements depend on the actual operations performed on the container.对元素施加的要求取决于在容器上执行的实际操作。 Generally, it is required that element type is a complete type and meets the requirements of Erasable , but many member functions impose stricter requirements (since C++11, until C++17).一般要求元素类型是完整类型,满足Erasable的要求,但是很多成员函数的要求比较严格(从 C++11 到 C++17)。

Raw arrays do not meet any of those requirements.原始数组不满足任何这些要求。

However, you may use:但是,您可以使用:

  1. std::list of std::array . std::list std::array std::list
  2. Create a struct that holds the array and then use std::list of the struct .创建一个保存数组的struct ,然后使用struct std::list

Unless you intend to add any behavior to the array, I would recomment using the first option.除非您打算向数组添加任何行为,否则我建议使用第一个选项。

using namespace std;

list<list<int>> matrix;
for (int row = 0; row < 3; row++) 
{
     std::list<int> rowList;
     for (int col = 0; col < 3; col++)
     {
          rowList.push_back(arr[row][col]);             
     }
     matrix.push_back(rowList);
}

Since elements of a matrix are more traditionally accessed directly (like an array), std::vector actually makes more sense.由于传统上更直接地访问矩阵的元素(如数组),因此std::vector实际上更有意义。 Similar to above, except replace list with vector .与上面类似,除了用vector替换list

vector<vector<int>> matrix;
matrix.resize(3);
for (int row = 0; row < 3; row++) 
{
     auto& row = matrix[row];
     row.resize(3);
     for (int col = 0; col < 3; col++)
     {
         row[col] = arr[row][col];             
     }
}

What if you did a list<array<int, x>> temp , (where x is the length of your array)?如果你做了一个list<array<int, x>> temp ,(其中x是你的数组的长度)怎么办?

You can then iterate through the array first, then use push_back to add the array to the back of the list.然后您可以先遍历数组,然后使用push_back将数组添加到列表的后面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM