Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://pastebin.com/BEQWTjw0 - linear version
- #include <iostream>
- template <typename T>
- void printArr(T* arr, int arrSize)
- {
- for (int i{ 0 }; i < arrSize; ++i)
- {
- std::cout << arr[i] << '\t';
- }
- std::cout << '\n';
- }
- template <typename T>
- void printArr(T** container, int ySize, int xSize)
- {
- for (int y{ 0 }; y < ySize; ++y)
- {
- printArr(container[y], xSize);
- }
- std::cout << '\n';
- }
- template <typename T>
- void fillArr(T** container, int ySize, int xSize)
- {
- int cnt{ 1 };
- for (int y{ 0 }; y < ySize; ++y)
- {
- for (int x{ 0 }; x < xSize; ++x)
- {
- container[y][x] = cnt++;
- }
- }
- }
- template <typename T>
- T** createArr2D(int ySize, int xSize)
- {
- T** container{ new T * [ySize] };
- for (int y{ 0 }; y < ySize; ++y)
- {
- *(container + y) = new T[xSize];
- }
- return container;
- }
- template <typename T>
- void deleteArr2D(T** container, int ySize)
- {
- for (int y{ 0 }; y < ySize; ++y)
- {
- delete[] *(container + y);
- }
- delete[] container;
- }
- template <typename T>
- void deleteColumnArr2D(T** container, int ySize, int& xSize, int columnPos)
- {
- --xSize;
- T* oldRow;
- for (int y{ 0 }; y < ySize; ++y)
- {
- oldRow = *(container + y);
- *(container + y) = new T[xSize];
- for (int x{ 0 }; x < xSize; ++x)
- {
- *(*(container + y) + x) = *(oldRow + x + (x >= columnPos));
- }
- delete[] oldRow;
- }
- }
- template <typename T>
- void addColumnArr2D(T** container, int ySize, int& xSize, int columnPos, T* newCol = nullptr)
- {
- T* oldRow;
- for (int y{ 0 }; y < ySize; ++y)
- {
- oldRow = *(container + y);
- *(container + y) = new T[xSize + 1];
- for (int x{ 0 }; x < xSize; ++x)
- {
- *(*(container + y) + x + (x >= columnPos)) = *(oldRow + x);
- }
- *(*(container + y ) + columnPos) = newCol ? *(newCol + y) : 0;
- delete[] oldRow;
- }
- ++xSize;
- }
- template <typename T>
- void deleteRowArr2D(T**& container, int& ySize, int xSize, int rowPos)
- {
- --ySize;
- T** newContainer{ new T * [ySize] };
- for (int y{ 0 }; y < ySize; ++y)
- {
- *(newContainer + y) = *(container + y + (y >= rowPos));
- }
- delete[] *(container + rowPos);
- delete[] container;
- container = newContainer;
- }
- template <typename T>
- void addRowArr2D(T**& container, int& ySize, int xSize, int rowPos, T* newRow = nullptr)
- {
- T** newContainer{ new T*[ySize + 1] };
- for (int y{ 0 }; y < ySize; ++y)
- {
- *(newContainer + y + (y >= rowPos)) = *(container + y);
- }
- *(newContainer + rowPos) = new T[xSize];
- for (int x{ 0 }; x < xSize; ++x)
- {
- *(*(newContainer + rowPos) + x) = newRow ? *(newRow + x) : 0;
- }
- ++ySize;
- delete[] container;
- container = newContainer;
- }
- int main()
- {
- int ySize{ 3 };
- int xSize{ 3 };
- auto arr2D{ createArr2D<int>(ySize,xSize) };
- fillArr(arr2D, xSize, ySize);
- printArr(arr2D, ySize, xSize);
- deleteColumnArr2D(arr2D, ySize, xSize, 2);
- printArr(arr2D, ySize, xSize);
- auto newCol{ new int[ySize] {1,2,3} };
- addColumnArr2D(arr2D, ySize, xSize, 0, newCol);
- printArr(arr2D, ySize, xSize);
- deleteRowArr2D(arr2D, ySize, xSize, 2);
- printArr(arr2D, ySize, xSize);
- auto newRow{ new int[xSize] {1,2,3} };
- addRowArr2D(arr2D, ySize, xSize, 2, newRow);
- printArr(arr2D, ySize, xSize);
- deleteArr2D(arr2D, ySize);
- delete[] newCol;
- delete[] newRow;
- return 0;
- }
Add Comment
Please, Sign In to add comment