gpt4 book ai didi

c++ - 将类型对列表的 vector 大小设置为用户在类中给定的大小

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:49 25 4
gpt4 key购买 nike

刚接触 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com