operator comparing multiple fields
I'd like to do it all by myself..
You should only compare the values of Obj::field2
if the values of Obj::field1
are equal.
The easy-to-understand way:
/* This will meet the requirements of Strict-Weak-Ordering */
if (a.field1 != b.field1) return a.field1 < b.field1;
else return a.field2 < b.field2;
The correct (recommended) way:
The "correct" way of implementing it uses only operator<
to compare the fields, the below looks more complicated than it really is.
It will however yield the same result as the easy-to-understand example previously written.
return a.field1 < b.field1 || (
!(b.field1 < a.field1) && a.field2 < b.field2
);
There must be a way of implementing operator<
without causing a lot of headache?
C++11
You can use std::tuple
from the STL which already have operator<
for multiple fields defined, such as in the below example.
#include <utility>
...
inline bool
operator< (Obj const& lhs, Obj const& rhs)
{
return std::tie (lhs.field1, lhs.field2) < std::tie (rhs.field1, rhs.field);
}
C++03
If your compiler doesn't have support for C++11 yet and you only need to compare two fields from each object you could use std::pair
instead.
The reason for std::make_pair
is the same as in the previous example using std::tie
.
#include <utility>
...
inline bool
operator< (Obj const& lhs, Obj const& rhs)
{
return std::make_pair (lhs.field1, lhs.field2)
< std::make_pair (rhs.field1, rhs.field2);
}
using std::pair
will require copies of the members to be created, which in some circumstances is undesirable.
Is this really recommended practise?
See the below question/answers for more information, but to sum it up; the c++11 approach doesn't cause that much overhead and it's very simple to implement.
- Implementing comparision operators via 'tuple' and 'tie', a good idea?
How to compare objects by multiple fields
You can implement a Comparator
which compares two Person
objects, and you can examine as many of the fields as you like. You can put in a variable in your comparator that tells it which field to compare to, although it would probably be simpler to just write multiple comparators.
Provide strict ordering during comparison with multiple fields
If you are on C++11 or higher, there is a nice trick to get SWO without having to write it out by hand. You can use std::tuple
, to pack your members and the fact that std::tuple
implements operator<
as a lexicographical ordering.
So you can write this
struct foo {
int x, y, z;
bool operator<(const foo& rhs) const {
return std::tie(x, y, z) < std::tie(rhs.x, rhs.y, rhs.y);
}
};
and struct foo
will be compared lexicographically by x, y, z. This is still a lot to write, so you can improve it a bit
struct foo {
int x, y, z;
auto tied() const {
return std::tie(x, y, z);
}
bool operator<(const foo& rhs) const {
return tied() < rhs.tied();
}
};
this can save a lot of typing if you have a lot of members, but it assumes C++14 (for C++11, you have to write out the return type of tied
by hand).
compare value with 2 different columns using the IN operator
One way to do it would be to use UNION if the columns are of the same type.
where @t in (select s1.RelationGDGMID from dbo.tblSettings s1 UNION
select s2.RelationGTTID from dbo.tblSettings s2)
The reason this works is because it is returning one value set (1 column with values). The reason where @t in (3, 1)
works is because this the same, it is returning one value set (value 3 and value 1).
That said I would prefer the EXISTS
over IN
as this could produce a better query plan.
Using OR in LIKE Query in MySQL to compare multiple fields
Use this::
SELECT * FROM MyTable WHERE (Column1 LIKE '%keyword1%' OR Column2 LIKE
'%keyword1%') AND (Column1 LIKE '%keyword2%' OR Column2 LIKE '%keyword2%');
Comparing more than 2 fields in Access query
If any 2 of the 3 fields are equal, you want the result to be "No"
.
So use the OR operator:
IIf(
[Field1] = [Field2] OR [Field1] = [Field3] OR [Field2] = [Field3],
"No",
"Yes"
) AS [MATCH]
Related Topics
Reading a Line from Ifstream into a String Variable
Why Does Same_As Concept Check Type Equality Twice
Pointer to Array with Const Qualifier in C & C++
Workaround for Template Argument Deduction in Non-Deduced Context
How to Pass a Function Pointer That Points to Constructor
How to Generate All Permutations of an Array in Sorted Order
Parameter Pack Must Be at the End of the Parameter List... When and Why
Where Are Temporary Object Stored
How to Properly Uninitialize Openssl
Opencv 2.3 C - How to Isolate Object Inside Image
How to Forward Declare a Class Which Is in a Namespace
Const Correctness for Value Parameters
C++: Rationale Behind Hiding Rule
Default Class Inheritance Access
Are There Practical Uses for Dynamic-Casting to Void Pointer
Gdb Shows Incorrect Arguments of Functions for Stack Frames
Are Static Variables in a Base Class Shared by All Derived Classes