Using an initializer_list
could look like this;
First #include <initializer_list>
std::vector<std::initializer_list<float>> vec{ {1,2,3} };
vec.push_back( {4,5,6} ); // add a row
Accessing each element could be done like;
for (auto list: vec){
for(auto element: list){
std::cout<< element << " "; // access each element
}
std::cout<<"\n";
}
Getting at an individual element with (x, y) coords;
// access first row (y coord = 0), second element (x coord = 1, also the column)
std::cout<< "vec[0].begin() + 1 = (addr:" << (vec[0].begin() + 1)
<< " - value: " << *(vec[0].begin() + 1) << ')';
All of that together would output;
1 2 3
4 5 6
vec[0].begin() + 1 = (addr:0x40a0d4 - value: 2)
A cleaner way could be done like this;
// using a variable type of initializer_list
std::initializer_list<float> row = {1,2,3};
std::vector<std::initializer_list<float>> vec{ row };
row = {4,5,6}; // change list
vec.push_back(row); // add rows
vec.push_back({7,8,9});
for (auto list: vec){
for(auto value: list){
std::cout<< value <<" ";
}
std::cout<<"\n";
}
//access without looping
const float *element = vec[0].begin();
// pointer to first row, first element (value: 1)
element+=3;
// point to second row, first element (value: 4)
std::cout<<"\nElement("<<*element<<")\n";
// access the same element with x,y coords = (0,1)
int x = 0, y = 1;
std::cout<<"\ncoord(0,1) = "<< *(vec[y].begin() + x) << "\n";
Would output;
1 2 3
4 5 6
7 8 9
Element(4)
coord(0,1) = 4
Problems i can think of with this (assuming it's of any worth) are that;
1) the data is initialized as constant floats
and as far as i know you cannot change them.
and
2) if you change the list
to equal {0,1,2,3,4,5}
you now have more than 3 columns.