我想知道在 cpp 類中初始化二維陣列的最佳方法是什么。在呼叫建構式之前我不知道它的大小,即,
頭檔案包含:
private:
int size;
bool* visited;
int edges;
int** matrix;
默認建構式(現在):
Digraph::Digraph(int n) {
int rows = (n * (n-1)/2);
int columns = 2;
matrix = new int[rows][2];
visited[size] = { 0 };
size = n;
edges = 0;
}
我想要的是一個 N 行 2 列的二維陣列。
error: cannot convert 'int (*)[2]' to 'int**' in assignment當我嘗試編譯時,這當前回傳。
注意:我不能使用 Vectors,所以請不要推薦它們。
uj5u.com熱心網友回復:
matrix = new int[rows][2];不是有效的語法。分配一個二維稀疏陣列需要多次new[]呼叫,例如:
private:
int size;
bool* visited;
int edges;
int** matrix;
int rows;
int columns;
...
Digraph::Digraph(int n) {
size = n;
edges = 0;
rows = (n * (n-1)/2);
columns = 2;
matrix = new int*[rows];
for(int x = 0; x < rows; x) {
matrix[x] = new int[columns];
for(int y = 0; y < columns; y)
matrix[x][y] = 0;
}
visited = new bool[size];
for(int x = 0; x < size; x)
visited[x] = false;
}
Digraph::~Digraph() {
for(int x = 0; x < rows; x) {
delete[] matrix[x];
}
delete[] matrix;
delete[] visited;
}
或者,考慮將 分配matrix為一維陣列,然后在訪問其值時使用二維索引,例如:
private:
int size;
bool* visited;
int edges;
int* matrix; // <-- 1 *, not 2 **
int rows;
int columns;
int& matrix_value(int row, int col) { return matrix[(row * rows) col]; }
...
Digraph::Digraph(int n) {
size = n;
edges = 0;
rows = (n * (n-1)/2);
columns = 2;
n = rows * columns;
matrix = new int[n];
for(int x = 0; x < n; x)
matrix[n] = 0;
visited = new bool[size];
for(int x = 0; x < size; x)
visited[x] = false;
}
Digraph::~Digraph() {
delete[] matrix;
delete[] visited;
}
無論哪種方式,您還需要根據 3/5/0 規則實作(或禁用)復制建構式和復制賦值運算子,最好是移動建構式和移動賦值運算子,例如:
Digraph::Digraph(const Digraph &src) {
size = src.size;
edges = src.edges;
rows = src.rows;
columns = src.columns;
matrix = new int*[rows];
for(int x = 0; x < rows; x) {
matrix[x] = new int[columns];
for (int y = 0; y < columns; y)
matrix[x][y] = src.matrix[x][y];
}
/* or:
n = rows * columns;
matrix = new int[n];
for(int x = 0; x < n; x)
matrix[n] = src.matrix[n];
*/
visited = new bool[size];
for(int x = 0; x < size; x)
visited[x] = src.visited[x];
}
Digraph::Digraph(Digraph &&src) {
size = 0;
edges = 0;
rows = 0;
columns = 0;
matrix = nullptr;
visited = nullptr;
src.swap(*this);
}
void Digraph::swap(Digraph &other) {
std::swap(size, other.size);
std::swap(edges, other.edges);
std::swap(rows, other.rows);
std::swap(columns, other.columns);
std::swap(matrix, src.matrix);
std::swap(visited, src.visited);
}
Digraph& Digraph::operator=(Digraph rhs) {
Digraph temp(std::move(rhs));
temp.swap(*this);
return this;
}
話雖如此,更好的設計是使用std::vector而不是new[], 并讓它為您處理所有記憶體管理和復制/移動,例如:
#include <vector>
private:
int size;
std::vector<bool> visited;
int edges;
std::vector<std::vector<int>> matrix;
// or: std::vector<int> matrix;
int rows;
int columns;
...
Digraph::Digraph(int n) {
size = n;
edges = 0;
rows = (n * (n-1)/2);
columns = 2;
matrix.resize(rows);
for(int x = 0; x < rows; x)
matrix[x].resize(columns);
/* or:
matrix.resize(rows * columns);
*/
visited.resize(size);
}
// implicitly-generated copy/move constructors, copy/move assignment operators,
// and destructor will suffice, so no need to implement them manually...
如果您不能使用std::vector,請考慮使用vector適當的語意實作您自己的類,然后改用它。您應該真正努力通過使用為您實作 3/5 規則的類來盡可能多地遵循 0 規則。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/371792.html
上一篇:如何回圈連接
