Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading for a set in c++

So I've created a new class called Tuples where Tuples takes in a vector of strings known as tupleVector. I then create a set of Tuples, meaning I need to overload the operators necessary to order elements and disallow duplicates.

Two questions:

  1. which operator overload is necessary? Do I overload < or ==?
  2. Assuming I must perform this overload within my Tuples class (so I can use the set of Tuples in other classes), is the following code correct?

    include "Tuples.h"
    Tuples::Tuples(vector<string> tuple){
        tupleVector = tuple;
    }
    vector<string> Tuples::getStrings()
    {
        return tupleVector;
    }
    bool Tuples::operator<(Tuples& other){
        return this->tupleVector<other.tupleVector;
    }
    bool Tuples::operator==(const Tuples& other)const{
        return this->tupleVector==other.tupleVector;
    }
    Tuples::~Tuples() {
        // TODO Auto-generated destructor stub
    }
    
like image 567
Adam Avatar asked Oct 14 '25 13:10

Adam


1 Answers

You only need to provide operator<. The container checks whether two items are equivalent by comparing them reflexively: they are equivalent if !(a<b) && !(b<a)

like image 153
Blaz Bratanic Avatar answered Oct 17 '25 01:10

Blaz Bratanic