简体   繁体   English

如何使用initializer_list

[英]How to use initializer_list

I have: 我有:

1) NetBeans 7.3 2) Qt by Digia v4.8.4. 1)NetBeans 7.3 2)Qt by Digia v4.8.4。 3) MinGW compiler. 3)MinGW编译器。

Could you help me initialize my array? 你能帮我初始化我的阵列吗? I have failed, unfortunately. 不幸的是,我失败了。 I understand that I have to use initializer_list. 我知道我必须使用initializer_list。 But what to include into my files and how to organize everything is a mystery to me. 但是包含在我的文件中以及如何组织所有内容对我来说是个谜。

Will you be so kind as to help me? 你愿意帮助我吗?

Figure.h Figure.h

#include <initializer_list>

class Figure: public QObject{
    Q_OBJECT
        private:        
                int shape[4][4][4];

Figure.cpp Figure.cpp

Figure:: Figure(){

std::initializer_list<int> init;
auto init = std::initializer_list<int>
(    
            {                        
                            {0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 0, 1, 0},
                    {0, 0, 0, 0}
                    ,                        
                            {0, 0, 0, 0}, 
                            {0, 0, 1, 0},
                            {0, 1, 1, 0},
                    {0, 0, 0, 0}
                    ,                        
                            {0, 0, 0, 0}, 
                            {0, 1, 0, 0},
                            {0, 1, 1, 0},
                    {0, 0, 0, 0}
                    ,                        
                            {0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 1, 0, 0},
                    {0, 0, 0, 0}                        
            }
    );  
...
  private:        
    int shape[4][4][4] {                        
                           {{0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 0, 1, 0},
                            {0, 0, 0, 0}}
                    ,                        
                           {{0, 0, 0, 0}, 
                            {0, 0, 1, 0},
                            {0, 1, 1, 0},
                            {0, 0, 0, 0}}
                    ,                        
                           {{0, 0, 0, 0}, 
                            {0, 1, 0, 0},
                            {0, 1, 1, 0},
                            {0, 0, 0, 0}}
                    ,                        
                           {{0, 0, 0, 0}, 
                            {0, 1, 1, 0},
                            {0, 1, 0, 0},
                            {0, 0, 0, 0}}                        
    };
...

In your particular case, you don't even need to use initalizer_list explicitly because static array can be initialized in the place of definition, and this is done via the syntax in the example above (requires C++11 compliance). 在您的特定情况下,您甚至不需要显式使用initalizer_list因为静态数组可以在定义位置初始化,这可以通过上面示例中的语法完成(需要符合C ++ 11)。

Initialization of shape in constructor initialization list is possible too: 也可以在构造函数初始化列表中初始化shape

...
Figure::Figure(): shape {
                               {{0, 0, 0, 0}, 
                                {0, 1, 1, 0},
                                {0, 0, 1, 0},
                                {0, 0, 0, 0}}
                        ,                        
                               {{0, 0, 0, 0}, 
                                {0, 0, 1, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}}
                        ,                        
                               {{0, 0, 0, 0}, 
                                {0, 1, 0, 0},
                                {0, 1, 1, 0},
                                {0, 0, 0, 0}}
                        ,                        
                               {{0, 0, 0, 0}, 
                                {0, 1, 1, 0},
                                {0, 1, 0, 0},
                                {0, 0, 0, 0}}
  } {
  ...
}
...

NOTE: Pay attention to the fact that you've missed additional parentheses in your try. 注意:请注意您在尝试中遗漏了额外的括号。

You do not need to create an initializer_list , especially because you would then be trying to assign it it to your array, rather than initialize your array from it - and that would be illegal. 您不需要创建initializer_list ,特别是因为您将尝试分配给您的数组,而不是从它初始化您的数组 - 这将是非法的。 Rather, you could initialize your array directly in the constructor's initialization list, as done below: 相反,您可以直接在构造函数的初始化列表中初始化数组,如下所示:

Figure::Figure()
    : shape
    {
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 0, 1, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 1, 0, 0},
            {0, 0, 0, 0}
        }
    }
{
    // Body of the constructor...
};

Notice that in C++11 you could also provide an inline initializer for your array, as done below: 请注意,在C ++ 11中,您还可以为数组提供内联初始值设定项,如下所示:

class Figure: public QObject {
    Figure();
private:
    int shape[4][4][4] {
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 0, 1, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 0, 0},
            {0, 1, 1, 0},
            {0, 0, 0, 0}
        },
        {
            {0, 0, 0, 0},
            {0, 1, 1, 0},
            {0, 1, 0, 0},
            {0, 0, 0, 0}
        }
    };
};

Figure::Figure() {
    // Body of the constructor...
}

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

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