efficient comparator for an std::map with a struct key - c++

I have a struct of following type which I am planning use as a key in a map.
Hence I write a comparator like below. I would like to know if there is a more elegant yet efficient way of doing this.
May be using std::pair or something.
struct T
{
int a, b, c, d;
bool operator< (const T& r) {
if (a < r.a)
return true
else if (a == r.a)
if (b < r.b)
return true;
else if (b == r.b)
if (c < r.c)
return true;
else if (c == r.c)
if (d < r.d)
return true;
return false;
}
}

Can you use C++11? If so:
struct T {
int a, b, c, d;
bool operator<(const T& rhs) const {
return tied() < rhs.tied();
}
private:
std::tuple<int, int, int, int> tied() const {
return std::make_tuple(a, b, c, d);
}
};
Alternatively, I would prefer returning at each possible opportunity to avoid the error-prone awkward nesting approach:
bool operator<(const T& rhs) const {
if (a != rhs.a) return a < rhs.a;
if (b != rhs.b) return b < rhs.b;
if (c != rhs.c) return c < rhs.c;
return d < rhs.d;
}

You can use...
bool operator<(const T& r)
{
return a < r.a ||
a == r.a && (b < r.b ||
b == r.b && (c < r.c ||
c == r.c && d < r.d));
}
Or...
return a != r.a ? a < r.a :
b != r.b ? b < r.b :
c != r.c ? c < r.c :
d < r.d;
You've said you're not using C++11, and Barry has a good illustration of a tuple approach, but for future reference and other interested parties, with a little reusable support code...
bool less_by_pairs()
{
return false;
}
template <typename T, typename U, typename ...Args>
bool less_by_pairs(const T& v1, const U& v2, Args... args)
{
return v1 != v2 ? v1 < v2 : less_by_pairs(args...);
}
...you can implement such operators more easily...
bool operator<(const T& r)
{
return less_by_pairs(a, r.a, b, r.b, c, r.c, d, r.d);
}
You could do something similar providing a list of members to compare, but the notation for that's actually a little verbose anyway.

You could use another field to store a key. This key value could be generated through a formula, that takes as input (a,b,c,d)
Like so:
void hash()
{
key = (a ^ b ^ c ^ d);
}
As result, you would need to only compare this key to know if the contents are the same.

Related

std::map with key as a struct with three int members [duplicate]

This question already has an answer here:
Efficient operator< with multiple members
(1 answer)
Closed 1 year ago.
I would like to use a struct with three integer members as a key. How can I overload the < operator. I understand that for two members it could be overloaded as:
bool operator < (const CacheKey& a, const CacheKey& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
The generic solution is:
if (a.x != b.x) return a.x < b.x;
if (a.y != b.y) return a.y < b.y;
// ...
return false;
Or:
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
(In this case, you might want to create a member function that returns the tied members, to be able to do something like a.tie() < b.tie() for all needed operators.)
Or, in C++20, you would add following to your class to automatically get all comparison operators including <:
auto operator<=>(const CacheKey &) const = default;
The most direct approach would be:
class Foo {
friend bool operator<(const Foo&, const Foo&);
int a, b, c;
}
bool operator<(const Foo& lhs, const Foo& rhs) {
return (lhs.a < rhs.a) ||
(lhs.a == rhs.a && lhs.b < rhs.b) ||
(lhs.a == rhs.a && lhs.b == rhs.b && lhs.c < rhs.c);
}

Appending map that uses Struct as a "multikey" and std::vector as mapped value

in code below it appends myList only for the first call of appendMyList(), and size stays 1, so can someone explain what is wrong here:
struct MyKey {
int accValue;
std::string name;
};
inline bool operator<(MyKey a, MyKey b){
return a.accValue < b.accValue && a.name < b.name;
}
inline bool operator==(MyKey a, MyKey b){
return a.accValue == b.accValue && a.name == b.name;
}
typedef std::map<MyKey, std::vector<int>> MyList;
class MyClass {
public:
void appendMyList(int accVal, std::string name, int element) {
myList[MyKey{accVal, name}].push_back(element);
cout<<endl<<"Size after"<< myList.size()<<endl;
}
MyList myList;
};
I saw similar post here but don't see anything wrong with my operators, so I guess it's something else?
This is how i call the function:
int main()
{
MyClass imHere;
int a = 1;
std::string yaya = "name";
for (int i = 0; i<10; i++) {
imHere.appendMyList((++a), yaya, i);
}
return 0;
};
std::map does not allow for storing duplicates. However, it doesn't use operator== for checking equality of elements. It uses operator< for both -- ordering elements as well as checking equivalence.
That is, if !(a < b) && !(b < a) then std::map considers a and b equivalent, hence no more elements than one are inserted using your operator<, because name is the same in all elements, thus both (a < b) and (b < a) return false.
Instead, you should be using:
inline bool operator<(MyKey a, MyKey b) {
return a.accValue < b.accValue || (a.accValue == b.accValue && a.name < b.name);
}
or:
inline bool operator<(MyKey a, MyKey b) {
return std::tie(a.accValue, a.name) < std::tie(b.accValue, b.name);
}

map with struct as key returns incorrect value

I have
struct data_cell{
int owner;
std::vector<int> shares;
};
data_cell** data; //grid, very big
std::map<data_cell, data_cell*> cells; //all uniq cells
bool operator==(const data_cell &b, const data_cell &o) {
return (b.owner == o.owner && b.shares == o.shares);
}
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares != o.shares);
}
int to_grid(float, float);
sometimes when I do:
for (int i=0;i<ids.size();i++){//example
data_cell o=ids[i];
//some work where fills o.shares
data[to_grid(x,y)]=(cells[o]?:cells[o]=(new data_cell(o)));
printf("%d |%d|%d|%d %d\n", s.id, o.owner, cells[o]->owner, data[to_grid(x,y)]->owner, to_grid(x,y));
}
I got that o.owner != cells[o]->owner, (printf shows different values), and if I print cells[o] before assign it returns nonzero pointer, but this key doesn't exist in map.
I use gcc-4.6.
what is wrong with this code?
Add1:
Change to
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares < o.shares);
}
doesn't help, but
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares <= o.shares);
}
Add2:
The last version that works(I hope):
bool operator<(const data_cell &b, const data_cell &o) {
return (b.owner < o.owner && b.shares <= o.shares) ||
(b.owner <= o.owner && b.shares < o.shares);
}
May be some additions?
Your == and < operators have to be such that exactly one of a < b, a == b, and b < a is true, and the other two false. That is not the case here due to b.shares != o.shares in your implementation of operator<.

std::set has duplicate entry

I have a set of tuples of 3 integers and I don't want any duplicates. That is, I don't want 2 entries with the same 3 values.
And here is my code.
struct Key{
unsigned a;
unsigned b;
unsigned c;
public:
Key(unsigned _a, unsigned _b, unsigned _c) :
a(_a),
b(_b),
c(_c) {}
bool operator<(const Key& rhs) const
{
if (a < rhs.a) {
return true;
}
if (b < rhs.b) {
return true;
}
if (c < rhs.c) {
return true;
}
return false;
};
};
std::set<Key> myset;
But I see duplicates in myset sometimes. I can't catch exactly what sequence causes the duplicate entry to be added. It doesn't always happen.
My question is this, is there something intrinsically wrong with my operator< function?
It's nearly right! But you are cascading too soon.
bool operator<(const Key& rhs) const
{
if (a < rhs.a)
return true;
if (a > rhs.a)
return false;
if (b < rhs.b)
return true;
if (b > rhs.b)
return false;
return (c < rhs.c);
};
Otherwise the following, for example, gives the wrong result:
Key lhs{3,1,0};
Key rhs{2,2,0};
assert(lhs < rhs); // passes, wrongly, because !(3 < 2) but then (1 < 2).
// you need an immediate `return false` when !(3 < 2)
It is safer to do something like this:
bool operator<(const Key& rhs) const
{
return std::tie(a, b, c) < std::tie(rhs.a, rhs.b, rhs.c);
}
C++'s standard library already knows what to do with that, so you don't have to.
Now, how can your bug lead to duplicate keys in a set?
Set's internal algorithm relies on the ordering being a strict weak ordering — when you break that precondition, you break the algorithms managing the internal tree, which is constructed and arranged using this ordering as its bible.
All hell breaks loose, basically. You could get a crash from this. In your case the symptoms were somewhat more benign (at least for now), with a deformed/mangled data tree resulting in the appearance of duplicated data.
It's folly to try to reason about the specific chain of events that led to a specific outcome, if you started off by breaking the preconditions and causing UB.
https://stackoverflow.com/a/979768/560648
Implementing comparison operators via 'tuple' and 'tie', a good idea?
Your operator<() is not consistent, as key1<key2 and key2<key1 could both be true (example: key1={1,3,0}, key2={3,1,0}). You should give the member variables a precedence in comparison:
if (a < rhs.a) {
return true;
} else if (a == rhs.a) {
if (b < rhs.b) {
return true;
} else if (b == rhs.b) {
if (c < rhs.c) {
return true;
}
}
}
return false;
You could indeed use standard class std::tuple as the key.
Nevertheless the operator can be defined the following way
bool operator <( const Key &rhs ) const
{
return
( a < rhs.a ) ||
( !( rhs.a < a ) && ( b < rhs.b ) ) ||
( !( rhs.a < a ) && !( rhs.b < b ) && ( c < rhs.c ) );
};
That this operator would work all you need is that for the type of objects a, b, and c there would be defined operator < Of course for arithmetic types it is already defined.
In fact it is the same as
#include <tuple>
//...
bool operator <( const Key &rhs ) const
{
return std::tie( a, b, c ) < std::tie( rhs.a, rhs.b, rhs.c );
}

How to find an element in a vector?

I have defined a structure Coord as
struct Coord {
int x;
int y;
int z;
};
Overloaded operator!= for Coord
bool Coord::operator !=(Coord& crd)const {
if(this->x != crd.x)
{
if(this->y != crd.y)
{
if(this->z != crd.z)
return true;
else
return false;
}
else
return false;
return true;
}
else
return false;
}
Then initialized a vector variable as
vector<Coord> vcoord;
Now I am using following code to get index of vector having a perticular Coord object
int Index::getVIndex(Coord crd) {
vector<Coord>::iterator it;
int indx;
it = vcoord.begin();
while(it != vcoord.end() && (*it) != crd)
++it;
indx = distance(vcoord.begin(),it);
cerr << (*it).x << " " << (*it).y << " " << indx << endl;
return indx;
}
But the value of indx is always 0. Kindly help to get correct result.
You need a not-equals operator for your Coord struct in order to be able to do this:
(*it) != crd
The logic of your not-equals operator is incorrect. The best and easiest option is to provide an equality comparison and use std::find:
struct Coord {
int x;
int y;
int z;
};
bool operator == (const Coord& lhs, const Coord& rhs)
{
return lhs.x==rhs.x && lhs.y==rhs.y && lhs.z==rhs.z;
}
You can then implement != in terms of ==, but you don't need it if you use std::find, which uses == by default:
vector<Coord>::iterator it = std::find(vcoord.begin(), vcoord.end(), crd);
Your != operator returns true only if all coordinates differ; it should return true if any differ. This means your function will return zero if any coordinate of the first element matches the function argument's.
Your version is a long-winded way of writing:
return x != crd.x && y != crd.y && z != crd.z;
when it should be:
return x != crd.x || y != crd.y || z != crd.z;
It may be easier to get the logic correct by implementing it in terms of ==:
bool operator==(Coord const & lhs, Coord const & rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
}
bool operator!=(Coord const & lhs, Coord const & rhs) {
return !(lhs == rhs);
}
Also, given a definition of ==, you can use std::find rather than rolling your own loop:
auto found == std::find(vcoord.begin(), vcoord.end(), crd);
if (found == vcoord.end()) {
// Decide what to do if not found.
// Returning zero is a bad idea, since there's no way distinguish that
// from a successful outcome.
} else {
return std::distance(vcoord.begin(), found);
}
You incorrectly implemented the logic in the inequality operator.
It should be
bool Coord::operator !=(const Coord& crd)const {
return x != crd.x || y != crd.y || z != crz.z;
}
Your implementation is logically equivalent to
return x != crd.x && y != crd.y && z != crz.z;