包含列表的表

时间:2015-02-02 16:28:13

标签: c++ arrays pointers

我有一个问题。如果想要创建一个3乘3的表,其中每个对象都是一个字符列表容器。我该怎么办?

我尝试使用双指针但是我无法在矩阵中推送我的列表。 使用三重指针,我完全迷失了......我试过了;

char ***obj;
obj = new char **[3]; 
for(int i=0; i<3; i++){
        obj[i] = new char*[3];}
for(int i=0; i<3; i++)
        for(int j=0; j<3; j++){
            obj[i][j] = new char;}

但是现在当我要求obj的一个元素时,它不仅输出与索引相对应的列表,而且还输出所有后续的...我真的很感激一些澄清!

2 个答案:

答案 0 :(得分:1)

使用std::array代替Hades的指针。

#include <array>
#include <list>

typedef std::array<std::array<std::list<char>,3>,3> ArrayList;

int main()
{
  ArrayList myList;  
  myList[0][0].push_back('a'); // add the letter 'a' to the list located at (0,0)
}

答案 1 :(得分:0)

好的,谢谢。我做得很糟糕。在类中调用表可以解决我的问题,而不必使用很多高级功能。欢呼声

class Produits
{
    private:
    char *nom;
    Produits *tableau[3];
    public:
    Produits(){      
        for (int i=0; i<3; i++){
            tableau[i] = tableau[3];}}
    int Affichage(int A, int B){
        printf("%s", tableau[A][B].nom);}
};
相关问题