0

So I have a class UseCase:

class UseCase {
private:
    string entity;
    string relationship;
    string target;
public:
    UseCase();
    UseCase(string, string, string);
    string getEntity();
    string getRelationship();
    string getTarget();
};

UseCase::UseCase() {}

UseCase::UseCase(string entity, string relationship, string target) {
    this->entity = entity;
    this->relationship = relationship;
    this->target = target;
}

string UseCase::getEntity() {
    return entity;
};

string UseCase::getRelationship() {
    return relationship;
};

string UseCase::getTarget() {
    return target;
};

I then have a vector<UseCase*>, containing hundreds of pointers to generated UseCase objects. However, some of these objects contain the same entity, relationship and target attributes. What is the fastest or most efficient way to remove the duplicates from the vector of pointers?

I have done research but I can't find anything relevant to a vector/array of pointers.

Importing a library/using an advanced algorithm is ok. The solution doesn't not need to be simple, only efficient.

Thanks!


Marked as duplicate? This isn't a simple "delete duplicates in vector" question. It's a vector of pointers, and asking how to efficiently detect pointers to objects that are the same, and then delete those objects and the pointers to the object from the vector. I've spent over an hour on here and haven't found a solution

  • First consider if a `std::vector` is the best data structure to use here. It may be better to use something like `std::set>` for example. – sjrowlinson May 22 '16 at 16:04
  • 2
    *"This isn't a simple "delete duplicates in vector" question."* -- Yes, it is. Why do you think that the fact that you are dealing with pointers matters? – Benjamin Lindley May 22 '16 at 16:11
  • It may be worth searching how to sort vectors with pointers because you need to create a custom comparator. – Galik May 22 '16 at 16:16
  • In fact my answer here may be useful: https://stackoverflow.com/questions/34757448/sorting-a-vector-of-objects-alphabetically-in-c/34757492#34757492 – Galik May 22 '16 at 16:24

0 Answers0