In each new standard, there are more and more features and containers being added to C++
, they make the code looks better and makes the coding process easier and faster while keeping the flexibilty of the C
language. If some code takes twenty lines in C++03
, then in C++11
with new built-in algorithms, autos and lambda expressions it takes only few lines, and in C++14
we can even set a "deprecated" attribute to a function/class to prevent ugly pragmas and defines.
But, I wonder, why in C++11/14 standard library (STL) there's no containers for dynamic multi-dimensional arrays? Of course, i know that it can be done with nested vectors like that:
std::vector< std::vector< ... > >
or by allocating memory manually (malloc
, new
, etc.). The problem is, for example, when you try to add a row/column to an 2-dimensional array, it looks really ugly:
// to add a row
v.push_back(std::vector<T>(width));
// to add a column
std::for_each(m.begin(), m.end(),
[](std::vector<T>& a){ a.push_back(0); });
So, to add a column we have to manually iterate through all rows and add an element for each row, instead of doing some v.resize<dimension_number>(new_size, default_value)
.
To add a row we have to manually remember the width of our matrix, or do something like width = m[0].size()
, but, as for me, it looks ugly and unaesthetic.
Aucun commentaire:
Enregistrer un commentaire