Take this code snippet for example:
<code>#include <iostream>
#include "set"
using namespace std;
struct WeightedEdge {
public:
int weight;
int n1;
int n2;
WeightedEdge(int n1, int n2, int weight) {
this->n1 = n1;
this->n2 = n2;
this->weight = weight;
}
void print() {
cout << "WeightedEdge(" << "n1= " << n1 << ",n2= " << n2;
cout << ",weight= " << weight << ")" << endl;
}
bool operator<(const WeightedEdge &other) const {
if (weight < other.weight) {
return true;
} else if (weight > other.weight) {
return false;
} else {
if (n1 < other.n1) {
return true;
} else if (n1 > other.n1) {
return false;
} else {
if (n2 < other.n2) {
return true;
} else if (n2 > other.n2) {
return false;
} else {
return false;
}
}
}
}
};
int main() {
set<WeightedEdge> edges;
edges.insert(WeightedEdge(1, 2, 5));
edges.insert(WeightedEdge(1, 12, 5));
edges.insert(WeightedEdge(1, 5, 5));
edges.insert(WeightedEdge(1, 3, 5));
edges.insert(WeightedEdge(1, 2, 5));
for (auto i = edges.begin(); i != edges.end(); ++i) {
auto edge = *i;
edge.print();
}
}
</code>
<code>#include <iostream>
#include "set"
using namespace std;
struct WeightedEdge {
public:
int weight;
int n1;
int n2;
WeightedEdge(int n1, int n2, int weight) {
this->n1 = n1;
this->n2 = n2;
this->weight = weight;
}
void print() {
cout << "WeightedEdge(" << "n1= " << n1 << ",n2= " << n2;
cout << ",weight= " << weight << ")" << endl;
}
bool operator<(const WeightedEdge &other) const {
if (weight < other.weight) {
return true;
} else if (weight > other.weight) {
return false;
} else {
if (n1 < other.n1) {
return true;
} else if (n1 > other.n1) {
return false;
} else {
if (n2 < other.n2) {
return true;
} else if (n2 > other.n2) {
return false;
} else {
return false;
}
}
}
}
};
int main() {
set<WeightedEdge> edges;
edges.insert(WeightedEdge(1, 2, 5));
edges.insert(WeightedEdge(1, 12, 5));
edges.insert(WeightedEdge(1, 5, 5));
edges.insert(WeightedEdge(1, 3, 5));
edges.insert(WeightedEdge(1, 2, 5));
for (auto i = edges.begin(); i != edges.end(); ++i) {
auto edge = *i;
edge.print();
}
}
</code>
#include <iostream>
#include "set"
using namespace std;
struct WeightedEdge {
public:
int weight;
int n1;
int n2;
WeightedEdge(int n1, int n2, int weight) {
this->n1 = n1;
this->n2 = n2;
this->weight = weight;
}
void print() {
cout << "WeightedEdge(" << "n1= " << n1 << ",n2= " << n2;
cout << ",weight= " << weight << ")" << endl;
}
bool operator<(const WeightedEdge &other) const {
if (weight < other.weight) {
return true;
} else if (weight > other.weight) {
return false;
} else {
if (n1 < other.n1) {
return true;
} else if (n1 > other.n1) {
return false;
} else {
if (n2 < other.n2) {
return true;
} else if (n2 > other.n2) {
return false;
} else {
return false;
}
}
}
}
};
int main() {
set<WeightedEdge> edges;
edges.insert(WeightedEdge(1, 2, 5));
edges.insert(WeightedEdge(1, 12, 5));
edges.insert(WeightedEdge(1, 5, 5));
edges.insert(WeightedEdge(1, 3, 5));
edges.insert(WeightedEdge(1, 2, 5));
for (auto i = edges.begin(); i != edges.end(); ++i) {
auto edge = *i;
edge.print();
}
}
I don’t know that i understanded how insertion in sets works in c++ but it seems like it uses the less than oparator(by default) to compare and place the inserted element correctly.
However i can’t understand how does it detects duplication.
If we insert(1,2,5) and (1,12,5) in the set, the opeator function returns false when its comparing n2 attributes.
But when we try to insert (1,2,5) and (1,2,5), the operator also returns false.
So how does the set decide to insert the (1,4,5) but not the second (1,2,5) while the operator retuned false anyways?