Why does defining a struct with an operand< function that isn't const break things if you use it as a key in a Map? - c++

I use a map and within that map, the type of the key is Coordinates:
struct Coordinates
{
int x;
int y;
Coordinates()
{
x = -1;
y = -1;
};
Coordinates(int a, int b) : x(a), y(b) {};
bool operator<(const Coordinates& otherCords) const
{
int thisSize;
int otherSize;
if (x >= y)
{
thisSize = x - y;
}
else
{
thisSize = y - x;
}
if (otherCords.x >= otherCords.y)
{
otherSize = otherCords.x - otherCords.y;
}
else
{
otherSize = otherCords.y - otherCords.x;
}
return thisSize < otherSize;
}
};
Took my quite a while to realize my operand function wasn't being detected by the map because it wasn't const. Why is that so?

Short answer: because that's the requirement of the map class.
Longer answer: The keys of a map are const and cannot be modified (because this could break the sort order the map relies on). Since the keys are constant values, any comparison function used with them needs to be const.

It must be const since you're not allowed to change the value of the key while it's in the map. In order to change a key, you must extract the element and reinsert it with a new key.
A bigger problem is that your operator< does not fulfill the strict weak ordering requirement - and many coordinates are likely to get rejected when you try to enter them into the map because an equal coordinate already exists. According to your function:
{0,0} == {1,1} == {2,2} == {3,3} // all where std::abs(x-y) == 0 are equal
{0,1} == {1,0} == {1,2} == {2,1} // all where std::abs(x-y) == 1 are equal
{0,2} == {2,0} == {1,3} == {3,1} // all where std::abs(x-y) == 2 are equal
...and so on...
One remedy could be to change the comparison function to:
bool operator<(const Coordinates& otherCords) const {
if(x==otherCords.x) return y < otherCords.y;
return x < otherCords.x;
}
Or simpler:
#include <tuple>
...
bool operator<(const Coordinates& otherCords) const {
return std::tie(x,y) < std::tie(otherCords.x, otherCords.y);
}

Related

C++ recursive struct comparator

I have created a struct to use as a key in a map to avoid having duplicate elements.
The struct contains pointers to children and siblings of its own type.
For the map, I have created a custom comparator that is supposed to recursively look at the element, the children and the siblings until a difference is found to make sure the elements are the same.
However, for some reason it is not working and Im still getting duplicates. After checking them out in the debugger, I concluded that they are indeed the exact same through and through so the problem must probably be somewhere in there.
This is the struct.
struct controlIdentifier
{
DWORD m_dwID;
DWORD m_dwDefaultID;
DWORD m_dwDisableID;
BYTE m_bType;
int m_nWidth;
int m_nHeight;
int m_nMargineH;
int m_nMargineV;
shared_ptr<controlIdentifier> m_pCHILD;
shared_ptr<controlIdentifier> m_pNEXT;
bool operator<(const controlIdentifier& id) const
{
if (m_dwDefaultID < id.m_dwDefaultID)
return true;
if (m_dwDisableID < id.m_dwDisableID)
return true;
if (m_bType < id.m_bType)
return true;
if (m_nWidth < id.m_nWidth)
return true;
if (m_nHeight < id.m_nHeight)
return true;
if (m_nMargineH < id.m_nMargineH)
return true;
if (m_nMargineV < id.m_nMargineV)
return true;
if (!m_pCHILD && id.m_pCHILD)
return true;
if (m_pCHILD && !id.m_pCHILD)
return false;
if (!m_pNEXT && id.m_pNEXT)
return true;
if (m_pNEXT && !id.m_pNEXT)
return false;
bool smaller = false;
if (m_pCHILD && id.m_pCHILD)
smaller = *m_pCHILD < *id.m_pCHILD;
if (!smaller)
{
if (m_pNEXT && id.m_pNEXT)
return *m_pNEXT < *id.m_pNEXT;
}
else
return smaller;
return false;
}
};
And this is how it's used.
struct cmpBySharedPtr {
bool operator()(const shared_ptr<controlIdentifier>& a, const shared_ptr<controlIdentifier>& b) const {
return *a < *b;
}
};
std::set<FRAMEDESC_SHAREDPTR> m_curFrames;
std::map<shared_ptr<controlIdentifier>, FRAMEDESC_SHAREDPTR, cmpBySharedPtr> m_serialFrames;
for (auto&& frame : m_curFrames)
{
shared_ptr<controlIdentifier> id;
makeIdentifiers(frame, id);
id->m_dwID = newId;
auto find = m_serialFrames.find(id);
if (find == m_serialFrames.end())
{
m_serialFrames.insert(std::pair(id, frame));
newId++;
}
}
m_dwID is not being compared on purspose.
Consider A = (child = 5, next = 6) and B = (child = 6, next = 5). Now A<B is true as (A.child < B.child) is true and it just returns that. Now consider B<A. B.child < A.child is false, so it checks the next fields.. Now B.next < A.next is true, so your comparison returns true.
So this is nonsensical -> A<B is true and B<A is true. This means your comparator is invalid.
The technical term for this is the comparator requires strict weak ordering - see https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings. Your comparator breaks the asymmetry requirement.
You can construct operator < by comparing field by field. But what you did is too little. Basically it shall look like this:
bool operator < (const A& left, const A& right)
{
if (left.firstField < right.firstField) return true;
if (right.firstField < left.firstField) return false; // this case is missing
if (left.secondField < right.secondField) return true;
if (right.secondField < left.secondField) return false; // this case is missing
....
return false;
}
You are missing cases when you can conclude, that for sure, left object is "greater" than right object.

Puzzled by Sort comparison method in C++

I tried two comparison methods, and did not get expected result.
The first comparison method is inside of a customized class MyTreeNode as operator<; The second one is a new comparison class compare with an override method operator()(MyTreeNode*) .
The code is shown below. Both outputs are:
5 6 1
while the expected order should be 1 5 6.
The rule for the ordering is: If two nodes have the same x, then the node with a larger y value comes first. If nodes have equal both x and y, then the node with less treeNode->val comes first.
So, can anyone help explain it for me ? Thanks
#include <vector>
#include <cstddef>
#include <algorithm>
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x) {}
};
class MyTreeNode{
public:
TreeNode* treeNode;
int x;
int y;
public:
MyTreeNode(TreeNode* node, int _x, int _y): treeNode(node), x(_x), y(_y){
}
//Solution 1.
bool operator<(MyTreeNode& node){
if(x< node.x){
return true;
}
if(x == node.x && y > node.y){
return true;
}
if(x == node.x && y == node.y
&&treeNode->val<node.treeNode->val){
return true;
}
return false;
}
};
//Solution 2
class compare{
public:
bool operator()(MyTreeNode* node1, MyTreeNode* node2){
if(node1->x < node2->x){
return true;
}
if(node1->x == node2->x && node2->y > node1->y){
return true;
}
if(node1->x == node2->x && node2->y == node1->y
&&node1->treeNode->val<node2->treeNode->val){
return true;
}
return false;
}
};
int main(int argc, char* argv[]){
//Solution so;
vector<MyTreeNode*> trees;
trees.push_back(new MyTreeNode(new TreeNode(5), 0, -2)); //A
trees.push_back(new MyTreeNode(new TreeNode(6), 0, -2)); //B
trees.push_back(new MyTreeNode(new TreeNode(1), 0, 0)); //C
//Solution 1
sort (trees.begin(), trees.end());
//Solution 2
//sort (trees.begin(), trees.end(), compare()); // print 5 6 1
// for(int i=0; i<res.size(); i++){
for_each(trees.begin(), trees.end(), [](const MyTreeNode* ele){cout<< " "<< ele->treeNode->val ;});
//}
}
```
1. If you want to reverse the order so that
the node with a larger y value comes first,
you should reverse the order in the comparator. Namely, instead of
node2->y > node1->y (which is equivalent to node1->y < node2->y) you should write node2->y < node1->y.
2. Note that the line sort(trees.begin(), trees.end());
will sort elements by their pointer values*, not by element values. The operator MyTreeNode::operator< won't be used - comparison between MyTreeNode* values won't be magically translated into comparison between dereferenced MyTreeNode values. That's not what you want. Use a custom comparator, as in your solution 2.
If you define MyTreeNode::operator<, then you can use a simple lambda as a comparator:
std::sort(trees.begin(), trees.end(), [](auto n1, auto n2) { return *n1 < *n2; });
* std::sort uses operator< to compare elements. The < comparison between two unrelated pointers (which do not point to elements of the same array) is undefined behaviour. std::less should be used as a custom comparator to avoid UB even if you want to sort pointers by their values (addresses).

error: passing ‘const Integer’ as ‘this’ argument of ‘Integer Integer::pow(int)’ discards qualifiers

I have Integer class that is supposed to simulate an integer mod n. Thus, it has constructors like:
Integer::Integer(int x)
: m(x), n(0)
{
}
Integer::Integer(int x, int y)
: n(y), m(x)
{
// if this->n greater than 1
if (this->n > 1)
{
// mod this->m by this->n
this->m %= this->n;
// if this->m is negative
if (this->m < 0)
{
// add this->n to it
this->m += this->n;
}
}
}
There is also Integer::inverse(), Integer::pow(int), which are needed for completeness of this question:
Integer Integer::inverse()
{
// Extended Euclidean Algorithm
int t = 0,
r = this->n,
newT = 1,
newR = this->m;
while (newR != 0)
{
int quotient = r / newR,
tCopy = t,
rCopy = r,
newTCopy = newT,
newRCopy = newR;
t = newT;
newT = tCopy - quotient * newTCopy;
r = newR;
newR = rCopy - quotient * newRCopy;
}
if (r > 1)
{
throw Integer(-1);
}
if (t < 0) t = t + this->n;
return Integer(t, this->n);
}
Integer Integer::squared()
{
return Integer(this->m * this->m, this->n);
}
Integer Integer::pow(int x)
{
// if x less than 0, return this->inverse().pow(-x)
if (x < 0) return this->inverse().pow(-x);
// if x is 0, return Integer(1)
if (x == 0) return Integer(1, this->n);
// if x is 1, return *this
if (x == 1) return *this;
// if x is 2, return this->squared()
if (x == 2) return this->squared();
// if x greater than 2
if (x > 2)
{
// if x is even
if (x % 2 == 0)
{
// return this->pow(x/2).squared()
return this->pow(x/2).squared();
}
// return this->pow(x/2).squared() * (*this)
return this->pow(x/2).squared() * (*this);
}
}
Problem I'm having is when I go to implement Integer::isQuadraticResidue() const:
bool Integer::isQuadraticResidue() const
{
// if this->n is zero
if (this->n == 0)
{
// this doesn't belong to Integers mod anything. check for perfect square instead
double baseSquareRoot = std::sqrt((double)this->m);
return (baseSquareRoot == (double)((int)baseSquareRoot));
}
// this is quadratic residue iff this->pow((this->n + 1) / 2) == Integer(1, this->n)
return (this->pow((n + 1) / 2).m == 1);
}
I get following error: error: passing ‘const Integer’ as ‘this’ argument of ‘Integer Integer::pow(int)’ discards qualifiers. I think it has everything to do with the const on the end. What do?
EDIT: Class header file looks something like:
#ifndef INTEGER_H
#define INTEGER_H
#include <iostream>
class Integer
{
public:
Integer(int);
Integer(int, int);
// functions
Integer inverse();
Integer squared();
Integer pow(int);
bool isQuadraticResidue() const;
Integer sqrt();
private:
int m, n;
};
#endif
This is an issue with const correctness, where a const function is trying to call a non-const function.
// ...
bool Integer::isQuadraticResidue() const;
Integer Integer::pow(int x);
// ....
In this situation, this is an Integer* in pow(), and a const Integer* in isQuadraticResidue(); this means that pow() can call isQuadraticResidue(), because it's legal to add CV-qualifiers, but not the other way around (because pow() would have to accept this as a non-qualified Integer*, losing the const qualifier).
This is unallowed, because allowing it would mean that isQuadraticResidue() breaks its guarantee that it won't modify the instance, whether directly or indirectly. While it itself doesn't change state, it assumes that pow() does change state, because pow() isn't also const (and thus doesn't promise not to change state). Due to this, isQuadraticResidue() is unable to call pow(), because doing so would risk breaking its guarantee.
Considering that, there are two solutions for this issue.
Remove const from isQuadraticResidue(). This would naively solve the problem, but isn't recommended, because then you wouldn't be able to reap the benefits of const correctness.
Make all member functions that don't modify Integer's logical state const, as well. This will require a bit more effort, but is overall safer. As they can then be called on a const Integer just as well as they can on an Integer, you will then be able to pass your instances around as const whenever they don't need to be modified, giving you a greater degree of safety.
Integer Integer::inverse() const;
Integer Integer::squared() const;
Integer Integer::pow() const;
This will need to be changed both in the functions' prototypes, and in their definitions.
the problem maybe you declared pow's this as non-const and trying to invoke it from isQuadraticResidue()const so in c++ it is not allowed to do so:
const this calls only member function whose this is const
you should either make pow's this const and as I guess it is bad idea becuase maybe you want to change some member data in it.
otherwise make isQuadraticResidue's this non-const
Integer pow(int x); // this is non const
bool isQuadraticResidue(); // this is not const
now everything is ok. as you can see after adding class declaration we get the error where!

C++ std::set<string> Alphanumeric custom comparator

I'm solving a problem with a sorting non-redundant permutation of String Array.
For example, if input string is "8aC", then output should be order like {"Ca8","C8a", "aC8", "a8C", "8Ca", "9aC"}.I chose C++ data structure set because each time I insert the String into std:set, set is automatically sorted and eliminating redundancy. The output is fine.
But I WANT TO SORT SET IN DIFFERENT ALPHANUMERIC ORDER which is different from default alphanumeric sorting order. I want to customize the comparator of set the order priority like: upper case> lower case > digit.
I tried to customize comparator but it was quite frustrating. How can I customize the sorting order of the set? Here's my code.
set<string, StringCompare> setl;
for (i = 0; i < f; i++)
{
setl.insert(p[i]); //p is String Array. it has the information of permutation of String.
}
for (set<string>::iterator iter = setl.begin(); iter != setl.end(); ++iter)
cout << *iter << endl; //printing set items. it works fine.
struct StringCompare
{
bool operator () (const std::string s_left, const std::string s_right)
{
/*I want to use my character comparison function in here, but have no idea about that.
I'm not sure about that this is the right way to customize comparator either.*/
}
};
int compare_char(const char x, const char y)
{
if (char_type(x) == char_type(y))
{
return ( (int) x < (int) y) ? 1 : 0 ;
}
else return (char_type(x) > char_type(y)) ? 1 : 0;
}
int char_type(const char x)
{
int ascii = (int)x;
if (ascii >= 48 && ascii <= 57) // digit
{
return 1;
}
else if (ascii >= 97 && ascii <= 122) // lowercase
{
return 2;
}
else if (ascii >= 48 && ascii <= 57) // uppercase
{
return 3;
}
else
{
return 0;
}
}
You are almost there, but you should compare your string lexicographically.
I roughly added small changes to your code.
int char_type( const char x )
{
if ( isupper( x ) )
{
// upper case has the highest priority
return 0;
}
if ( islower( x ) )
{
return 1;
}
if ( isdigit( x ) )
{
// digit has the lowest priority
return 2;
}
// something else
return 3;
}
bool compare_char( const char x, const char y )
{
if ( char_type( x ) == char_type( y ) )
{
// same type so that we are going to compare characters
return ( x < y );
}
else
{
// different types
return char_type( x ) < char_type( y );
}
}
struct StringCompare
{
bool operator () ( const std::string& s_left, const std::string& s_right )
{
std::string::const_iterator iteLeft = s_left.begin();
std::string::const_iterator iteRight = s_right.begin();
// we are going to compare each character in strings
while ( iteLeft != s_left.end() && iteRight != s_right.end() )
{
if ( compare_char( *iteLeft, *iteRight ) )
{
return true;
}
if ( compare_char( *iteRight, *iteLeft ) )
{
return false;
}
++iteLeft;
++iteRight;
}
// either of strings reached the end.
if ( s_left.length() < s_right.length() )
{
return true;
}
// otherwise.
return false;
}
};
Your comparator is right. I would turn parameters to const ref like this
bool operator () (const std::string &s_left, const std::string &s_right)
and start by this simple implementation:
return s_left < s_right
This will give the default behaviour and give you confidence you are on the right track.
Then start comparing one char at the time with a for loop over the shorter between the length of the two strings. You can get chars out the string simply with the operator[] (e.g. s_left[i])
You're very nearly there with what you have.
In your comparison functor you are given two std::strings. What you need to do is to find the first position where the two strings differ. For that, you can use std::mismatch from the standard library. This returns a std::pair filled with iterators pointing to the first two elements that are different:
auto iterators = std::mismatch(std::begin(s_left), std::end(s_left),
std::begin(s_right), std::end(s_right));
Now, you can dereference the two iterators we've been given to get the characters:
char c_left = *iterators.first;
char c_right = *iterators.second;
You can pass those two characters to your compare_char function and it should all work :-)
Not absoloutely sure about this, but you may be able to use an enumerated class towards your advantage or an array and choose to read from certain indices in which ever order you like.
You can use one enumerated class to define the order you would like to output data in and another that contains the data to be outputed, then you can set a loop that keeps on looping to assign the value to the output in a permuted way!
namespace CustomeType
{
enum Outs { Ca8= 0,C8a, aC8, a8C, 8Ca, 9aC };
enum Order{1 = 0 , 2, 3 , 4 , 5};
void PlayCard(Outs input)
{
if (input == Ca8) // Enumerator is visible without qualification
{
string[] permuted;
permuted[0] = Outs[0];
permuted[1] = Outs[1];
permuted[2] = Outs[2];
permuted[3] = Outs[3];
permuted[4] = Outs[4];
}// else use a different order
else if (input == Ca8) // this might be much better
{
string[] permuted;
for(int i = 0; i<LessThanOutputLength; i++)
{
//use order 1 to assign values from Outs
}
}
}
}
This should work :
bool operator () (const std::string s_left, const std::string s_right)
{
for(int i = 0;i < s_left.size();i++){
if(isupper(s_left[i])){
if(isupper(s_right[i])) return s_left[i] < s_right[i];
else if(islower(s_right[i]) || isdigit(s_right[i]))return true;
}
else if(islower(s_left[i])){
if(islower(s_right[i])) return s_left[i] < s_right[i];
else if(isdigit(s_right[i])) return true;
else if(isupper(s_right[i])) return false;
}
else if(isdigit(s_left[i])){
if(isdigit(s_right[i])) return s_left[i] < s_right[i];
else if(islower(s_right[i]) || isupper(s_right[i])) return false;
}
}
}

QMultiMap with struct as key and struct as value, comparison operators

I have two structs, for example, first struct as key in QMultiMap:
struct KeyStruct
{
int keydata1;
int keydata2;
int keydata3;
bool operator < ( const KeyStruct& o ) const
{
return keydata1 < o.keydata1 || keydata2 < o.keydata2 || keydata3 < o.keydata3;
}
};
And second struct as value:
struct ValueStruct
{
int valuedata1;
QString valuedata2;
};
I tried to use QMultiMap like this:
multiMap.insert( keyStruct, valueStruct ); // in a loop
foreach( keyStruct, multiMap.keys()){
foreach( valueStruct, multiMap.values( keyStruct )){
// it never reaches this line
}
}
I'm shure in some cases I have multiple values per key, but can't even get one. I think the problem is in comparsion.
Also, I tried this:
bool operator == ( const KeyStruct& o ) const
{
return keydata1 == o.keydata1 &&
keydata2 == o.keydata2 &&
keydata3 == o.keydata3;
}
But it seems QMultiMap just doesn't use this code in my case.
Any data structure ( to my knowledge) that requires an operator<() to function has also the condition that:
A<B => ! B<A
This seems trivial, but in your case, it is possible that A<B && B<A. For instance if A.keydata1 < B.keydata1 but A.keydata2 > B.keydata2.
To fix this you should write your operator something like this:
bool operator < ( const KeyStruct& o ) const
{
if(keyData1 != o.keyData1) return keyData1 < o.keyData1;
if(keyData2 != o.keyData2) return keyData2 < o.keyData2;
//etc
return false; //they are equal
}