如何多次下载std :: array?

时间:2014-09-10 15:08:40

标签: c++ c++11 multidimensional-array stl

即使所有std::array返回都是引用,多次下载如何为operator[]工作,而不使用任何代理对象(如here所示)?

示例:

#include <iostream>
#include <array>

using namespace std;

int main()
{

    array<array<int, 3>, 4> structure;
    structure[2][2] = 2;
    cout << structure[2][2] << endl;

    return 0;
}

这是如何/为何有效?

2 个答案:

答案 0 :(得分:6)

您只需致电structure.operator[](2).operator[](2),其中第一个operator[]会返回structure中应用了第二个operator[]的第三个数组的引用。

请注意,对象的引用可以与对象本身完全相同。

答案 1 :(得分:5)

正如您所说,structure[2]提供了对外部数组元素的引用。作为参考,它可以被视为完全相同的类型的对象。

该元素本身就是一个数组(array<int,3>),可以应用另一个[]。这提供了对内部数组元素的引用,类型为int