如何填充具有类列表类型的矩阵: [英] how to fill a matrix that has a type of class list :

查看:105
本文介绍了如何填充具有类列表类型的矩阵:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何填充具有类列表类型的矩阵:



how to fill a matrix that has a type of class list :

list<ClassName*> mat[5][5];





和PLZ告诉我如何访问这个类列表:)



and PLZ tell me how to access this list of class :)

推荐答案

两个嵌套的for循环怎么样?例如。
How about two nested for-loops? E.g.
for (size_t i = 0, i < 5, ++i) {
    for (size_t j = 0; j < 5; ++j) {
       mat[i][j] = ... // create item for cell [i][j]
    }
}

干杯

Andi

Cheers
Andi


我将假设您的代码中的类型列表引用 std :: list 。如果是这样,我建议你重新考虑使用std; 重新使用:使用这种简化通常一个好主意!



mat 是一个5乘5的列表矩阵,用于存储指向 ClassName <类型对象的指针/ code>。



此矩阵定义将为所有元素调用 list 的默认构造函数矩阵。如果您使用 std :: list ,这意味着您将拥有一个空列表矩阵。如果要将矩阵元素设置为任何其他值,可以手动创建列表,并将它们分配给相应的元素。



要访问特定列表,您可以简单地对任何矩阵使用索引对:

I will assume that the type list in your code refers to std::list. If so, I advise you to reconsider your use of using std; : it is generally not a good idea to use this simplification!

mat is a 5 by 5 matrix of lists that store pointers to objects of type ClassName.

This matrix definition will invoke the default constructor of list for all elements of the matrix. If you are using std::list, this means you'll have a matrix of empty lists. If you want to set your matrix elements to any other value, you have create the list(s) manually, and assign them to the appropriate element(s).

To access a specific list, you can simply use an index pair as normal for any matrix:
int row = 2;
int column = 4;
list<ClassName*> my_list = mat[row][column];



要访问此类列表中的元素,可以使用迭代器,正常情况下 std :: list


To access the elements within such a list, you use iterators, as normal for std::list:

std::list<ClassName*>::iterator current_listelement = my_list.begin();



或:


or:

std::list<ClassName*>::iterator current_listelement = mat[row][column].begin();



要访问实际对象,需要取消引用迭代器。请注意,您将获得一个指针,而不是一个引用:


To access the actual object, you need to dereference the iterator. Note that you will get a pointer, not a reference:

ClassName* p_my_object = *current_listelement;


这篇关于如何填充具有类列表类型的矩阵:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆