Here's a simple dynamic 2D array with a custom column number:
class TwoDArray
{
size_t NCols;
std::vector<std::string> data;
public:
explicit TwoDArray(size_t n) : NCols(n) { }
std::string & operator()(size_t i, size_t j) { return data[i * NCols + j]; }
const std::string & operator()(size_t i, size_t j) const { return data[i * NCols + j]; }
void set_number_of_rows(size_t r) { data.resize(NCols * r); }
void add_row(const std::vector<std::string> & row)
{
assert(row.size() == NCols);
data.insert(data.end(), row.begin(), row.end());
}
};
Using:
TwoDArray arr(5);
arr.set_number_of_rows(20);
arr(0, 3) = "hello";
arr(17,2) = "world";
. , , , ; .
(i,j), [i][j].