Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Set of intervals.
//g++ 4.9.3 #include <iostream> #include <set> #include <memory> #include <algorithm> struct interval { int start; int end; interval(int s, int e) : start(s), end(e) { } friend bool operator<(const interval& i1, const interval& i2) { return i1.start < i2.start; } }; template <typename T> struct unique_ptr_comp { bool operator()(const std::unique_ptr<T>& p1, const std::unique_ptr<T> &p2) { return *p1 < *p2; } }; template <typename It> It find_interval(It first, It last, int value) { // See explanation below. auto it = std::lower_bound(first, last, value, [](const std::unique_ptr<interval>& i1, int value) { return i1->start < value; }); if (it != last && (*it)->start == value) { return it; } --it; // Change this to: (*it)->end > value ? it : db.end() // ...if the upper bound of the interval are not included. return (*it)->end < value ? last : it; } int main () { std::set<std::unique_ptr<interval>, unique_ptr_comp<interval>> db; db.emplace(std::make_unique<interval>(14, 18)); db.emplace(std::make_unique<interval>(0, 5)); db.emplace(std::make_unique<interval>(9, 11)); db.emplace(std::make_unique<interval>(6, 7)); db.emplace(std::make_unique<interval>(21, 25)); db.emplace(std::make_unique<interval>(26, 27)); for (int i = 0; i < 30; ++i) { auto it = find_interval(db.begin(), db.end(), i); std::cout << i << ": "; if (it == db.end()) { std::cout << "Not found.\n"; } else { std::cout << "[" << (*it)->start << ", " << (*it)->end << "]\n"; } } }
run
|
edit
|
history
|
help
0
C++ Car Racing game framework 2
function pointer overload
error
MovConstrAssign2
Stats - Central Limit Theorem - Normal Distribution with multiple items
Subarray with 0 sum
Atul
341 30 - B
test111
at_multimap_example