0
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<climits>
using namespace std;

class DS{
    vector<pair<int,int>> *l;
public:

    DS(int n){
        l = new vector<pair<int, int>>;
    }

    create_disjointSubset(int n){
      for(int i=0;i<n;i++){
        l->push_back({i,NULL});
      }
    }

    int find(int curr){
        if(l[curr].second == NULL){
            return curr;
        }
        return 0;
    }
};

That's the code. I am just trying to create a class called DS (disjoint subsets), and I am also trying to create a vector of pairs to store two integer values. I am new to c++ and I don't know for sure what may be wrong with the code. The error, by the way, is in the function find, for some reason I cannot use l[curr].second or even l[curr].first. I would appreacite any hints or answers

Deepak C
  • 33
  • 5
  • you have to deref the pointer that is `l` – Daniel A. White Apr 27 '22 at 00:21
  • `vector> *l;` + the constructor are nonsense and are pains. Avoid using pointers: `vector> l;`. – 273K Apr 27 '22 at 00:28
  • 1
    A `vector` is basically a smart pointer to a dynamic buffer. Having a pointer to a vector is just plain odd. – Eljay Apr 27 '22 at 00:28
  • 1
    `l[cur]` doesn't work because `l` is a pointer, so the compiler thinks `l` points to the first element of an array of vectors, and you want the `cur`th vector. Just use `vector> l;` and remove the `new`. There is almost never a reason to dynamically allocate a `std::vector`. Overusing `new` is a common mistake for new developers, specially those coming into C++ with a background in object oriented languages like Java or C#. – François Andrieux Apr 27 '22 at 00:29
  • In general, [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Apr 27 '22 at 00:36
  • @FrançoisAndrieux it explained a lot, and it is working fine now:) thank youu:) – Deepak C Apr 27 '22 at 00:42
  • One last tip: lower-case **L** (***`l`***) is not a good choice as an identifier; it can simply be mistaken with numeric literal **one** (***`1`***). – Red.Wave Apr 27 '22 at 07:47

0 Answers0