-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
44 lines (33 loc) · 1.01 KB
/
test.cpp
File metadata and controls
44 lines (33 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <set>
#include <iostream>
struct MyClass {
int currentValue;
struct MyComparator {
const MyClass* owner;
explicit MyComparator(const MyClass* owner) : owner(owner) {}
int operator()(int a, int b) const {
if (a * owner->currentValue < b * owner->currentValue) {
return -1; // a is less than b
} else if (a * owner->currentValue > b * owner->currentValue) {
return 1; // a is greater than b
}
return 0; // a and b are equal
}
};
std::set<int, MyComparator> mySet;
MyClass(int value) : currentValue(value), mySet(MyComparator(this)) {}
};
int main() {
MyClass obj(2);
obj.mySet.insert(3);
obj.mySet.insert(1);
obj.mySet.insert(2);
int target = 3;
auto it = obj.mySet.find(target);
if (it != obj.mySet.end()) {
std::cout << "Element found: " << *it << std::endl;
} else {
std::cout << "Element not found" << std::endl;
}
return 0;
}