1

just new to graphs in c++ stl sorry if its a beginner level question. please tell how can i set the size of vectors of list

#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
  }
};
Ayxan Haqverdili
  • 17,764
  • 5
  • 27
  • 57
  • Have you tried the reserve method [link to info on cppreference](https://en.cppreference.com/w/cpp/container/vector/reserve) – Toothless204 Nov 30 '19 at 18:46
  • Are you maybe looking for [std::vector::resize](https://en.cppreference.com/w/cpp/container/vector/resize)? – Jesper Juhl Nov 30 '19 at 18:47
  • @Toothless204 `reserve()` only changes `capacity()`, not `size()`. – Jesper Juhl Nov 30 '19 at 18:48
  • 1
    [Why should I not #include ?](https://stackoverflow.com/q/31816095/10147399) – Ayxan Haqverdili Nov 30 '19 at 18:58
  • ayxan is the collection of all c++ stl like algorithm, data structures like (stack,queue,list,vectors),iostream and many more just include this library and use any c++ stl function fluently. its like a collecction of libraries go see it at https://www.quora.com/What-is-the-file-bits-stdc++-h – Adnan Ahmed Dec 22 '19 at 07:54

1 Answers1

0

You can use resize() method.

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);
    }
};