我有一个名为Graph的类。在这里,这个顶点是类的成员。我已经在构造函数中初始化了顶点。此外,还有一个成员向量数组。我希望向量的数量等于顶点。例如,如果顶点=5,那么我的向量数组应该如下所示。向量v5;我如何在构造函数中做到这一点,因为我只知道构造函数中顶点的值?
class Graph
{
private:
int vertices;
std::vector<int> adj[];
public:
Graph(int v); //constructor
// add an edge
void addEdge(int u, int v);
//print bfs traversal of graph
void bfs(int s); // s is a source from where bfs traversal should
//start
};
Graph :: Graph(int v)
{
vertices = v;
}发布于 2018-09-28 23:20:43
因为您只能在运行时知道顶点的值,所以不能使用C样式的数组或std::array,因为它们需要在编译时知道大小。
您可以改用另一个向量:
std::vector<std::vector<int>> adj;https://stackoverflow.com/questions/52558338
复制相似问题