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

标签 c++ list vector graph

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

相关文章:

c++ - 在Visual Studio C++中使用Shark机器学习库

c++ - C++串联;字符串+双;运算符+与运算符+ =

c++ - Qt 网页程序集 : configure kit

c++ - EXPECT_CALL 在 Google 测试中没有模拟

c# - 如何从非常大的列表中按索引有效地删除元素?

C++ STL vector 半常数

java - 使用类名从 Java Vector 检索对象

c# - 将静态列表分配给非静态列表

python-3.x - 如何在回文中追加和加入中心元素(当出现不止一次时)

java - Eclipse 中未定义的 vector