Time appropriate greetings!
For something that I am working on I need a dynamic table of objects.
(So like a 2D array, but with vectors)
Because I don't know how many rows or columns are needed.
(Probably more columns then rows, maybe like a million rows and a hand full of columns, but I don't know yet ...)
A onedimensional vector is no problem, I know how to work with those.
(Use push_back(1) to append an element and stuff like that ...)
What I want to do is to have like a table with rows and columns.
Each "cell" of that table is an object (all of the same class).
How can I "add" one row or one column to that "table of objects" dynamically at runtime?
This is just an example of what I am trying to do:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
//The class of all objects in the table:
class Cell
{
int SomeValue;
int AnotherValue;
float RandomValue;
string AndAnotherRandomValue;
//Lots of other variables are here, most are integers)
}
//The "vector of vectors" (= 2D vector, right?)
vector < vector < Cell > > DynamicTable;
//A function that should work like push_back(1) on the rows of the table
void AddRowToDynamicTable()
{
//What do I need to put here?
}
//A function that should work like push_back(1) on the columns of the table
void AddColumnToDynamicTable()
{
//What do I need to put here?
}
|
And "reading / writing" stuff to each object in this "table" should work like this, right?
(After the 2D vector table has been resized to include those row & col values, of course)
1 2 3 4 5 6
|
//Reading the variable SomeValue from the object at row 10 column 24:
cout << DynamicTable[10,24].SomeValue;
//Writing into the variable RandomValue of the object at row 5 column 1:
DynamicTable[5,1].RandomValue = 9.852;
|
Right?
Or am I interpreting something wrong here?
If so, please let me know ...
Also, since this is quite a bit of data, should I consider writing and reading this stuff into and from a txt file, so that that RAM doesn't "run out"?
That would slow the programm down a lot, but that isn't really something that I care about, as long as it works.