Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int rows = 0;
- int columns = 0;
- int currentRowStorage = 5;
- char*** rowStringsPointer = nullptr;
- for (std::string line; std::getline(std::cin, line);) {
- // Set the number of columns from the first line and initialise an array
- // of 5 rows
- if (columns == 0) {
- columns = line.length();
- char** newRowArray = new char*[currentRowStorage];
- for (int i = 0; i < currentRowStorage; ++i) {
- newRowArray[i] = new char[columns + 1];
- }
- rowStringsPointer = &newRowArray;
- }
- // If the current array of rows is too small to add another
- if (rows + 1 > currentRowStorage) {
- // create a new array with capacity of old array + 5
- char** newRowArray = new char*[currentRowStorage + 5];
- // initialise each row as an empty array of column number of chars
- for (int i = 0; i < currentRowStorage + 5; ++i) {
- newRowArray[i] = new char[columns + 1];
- }
- // copy all rows from old array to new one and delete old rows
- for (int i = 0; i < currentRowStorage; ++i) {
- newRowArray[i] = *rowStringsPointer[i];
- delete *rowStringsPointer[i];
- }
- // delete old array
- delete *rowStringsPointer;
- // update pointer to point to new array
- rowStringsPointer = &newRowArray;
- }
- // create a new char array of length columns then copy the characters
- // from this line string
- char row[columns + 1];
- line.copy(row, columns + 1);
- row[columns + 1] = '\0';
- // add new row to array and increment rows counter
- *rowStringsPointer[rows] = row;
- ++rows;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement