刚接触 C++ 中的图形 STL 很抱歉,如果它是初学者级别的问题。请告诉我如何设置列表 vector 的大小
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
class my_graph {
public:
int vertices;
list<pair<int, int>> *adjacency_list;
vector<list<pair<int, int>>> adjacencyList;
my_graph(int ver) {
this->vertices = ver;
adjacency_list = new list<pair<int, int>>[vertices];
// what should i type here to make the size of
// vector type adjacencyList
// to user defined size in this constructor.
// just like i did for list type adjacency_list
}
};
最佳答案
您可以使用 resize() 方法。
class my_graph {
public:
int vertices;
list<pair<int, int>> *adjacency_list;
vector< list< pair<int, int> > > adjacencyList;
my_graph(int ver) {
this->vertices = ver;
adjacency_list = new list<pair<int, int>>[vertices];
adjacencyList.resize(vertices);
}
};
关于c++ - 将类型对列表的 vector 大小设置为用户在类中给定的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119137/