Created
October 16, 2025 12:19
-
-
Save lix19937/2eb5d94d3bf4966f75be643f373a278d to your computer and use it in GitHub Desktop.
find nearest value
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <queue> | |
| int main() { | |
| struct DrInfo { | |
| int64_t timestamp; | |
| }; | |
| std::queue<DrInfo> dr_queue_; | |
| std::queue<DrInfo> dr_queue2_; | |
| DrInfo dr_data; | |
| dr_data.timestamp = 179; | |
| dr_queue_.push(dr_data); | |
| dr_data.timestamp = 184; | |
| dr_queue_.push(dr_data); | |
| dr_data.timestamp = 192; | |
| dr_queue_.push(dr_data); | |
| dr_data.timestamp = 197; | |
| dr_queue_.push(dr_data); | |
| dr_queue2_ = dr_queue_; | |
| int64_t ref_time = 186; | |
| /// ------------------------------- | |
| { | |
| DrInfo current; | |
| while (!dr_queue_.empty()) { | |
| current = dr_queue_.front(); | |
| if (current.timestamp < ref_time && dr_queue_.size() > 1) { | |
| dr_queue_.pop(); | |
| } else { | |
| break; | |
| } | |
| } | |
| if (!dr_queue_.empty()) { | |
| printf("\t%lld\n", current.timestamp); | |
| } | |
| } | |
| /// ------------------------------- | |
| { | |
| DrInfo ldr, rdr; | |
| const int64_t init_v = 123456789; | |
| int64_t left_diff = init_v, right_diff = -init_v; | |
| while (!dr_queue2_.empty()) { | |
| const auto& dr = dr_queue2_.front(); | |
| if (dr.timestamp < ref_time) { | |
| left_diff = dr.timestamp - ref_time; | |
| ldr = dr; | |
| dr_queue2_.pop(); | |
| } else { | |
| rdr = dr; | |
| right_diff = dr.timestamp - ref_time; | |
| break; | |
| } | |
| } | |
| if (abs(left_diff) < right_diff) | |
| printf("\t%lld\n", ldr.timestamp); | |
| else if (abs(left_diff) == init_v) | |
| printf("\t%lld\n", rdr.timestamp); | |
| else if (abs(right_diff) == init_v) | |
| printf("\t%lld\n", ldr.timestamp); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment