How Do I define a Double Brackets/Double Iterator Operator, Similar to Vector of Vectors'?

后端 未结 5 853
温柔的废话
温柔的废话 2020-12-08 17:29

I\'m porting code that uses a very large array of floats, which may trigger malloc failures from c to c++. I asked a question about whether I should use vectors or deques a

5条回答
  •  情深已故
    2020-12-08 18:14

    There is no "double brackets" operator in C++. What you need to do is define a single [] operator and have it return a reference to another object, which can in turn respond to its own [] operator. This can be nested as many levels deep as you require.

    For example, when you create a vector of vectors, the [] operator on the outer vector returns a reference to one of the inner vectors; the [] operator on that vector returns a reference to an individual element of the vector.

    std::vector > example;
    std::vector & first = example[0];  // the first level returns a reference to a vector
    float & second = example[0][0];  // the same as first[0]
    

提交回复
热议问题