Created
November 18, 2022 11:30
-
-
Save ujjwal-kr/59234e996b2102e32effc8659cf796af to your computer and use it in GitHub Desktop.
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 <unordered_map> | |
| #include <string> | |
| #include <algorithm> | |
| void get_label(std::unordered_map<std::string, size_t> map, int pc) { | |
| std::vector<std::size_t> label_point_vec = {}; | |
| std::vector<std::string> key_vec = {}; | |
| for (const auto &pair : map) { | |
| label_point_vec.push_back(pair.second); | |
| key_vec.push_back(pair.first); | |
| } | |
| std::sort(label_point_vec.begin(), label_point_vec.end()); | |
| size_t final_point; | |
| std::string final_label; | |
| std::vector<size_t> point_stack; | |
| for (auto point : label_point_vec) { | |
| if (pc >= point) { | |
| point_stack.push_back(point); | |
| } | |
| } | |
| final_point = point_stack[point_stack.size() - 1]; | |
| for (auto& k : key_vec) { | |
| auto n = map.find(k); | |
| if (n->second == final_point) { | |
| final_label = k; | |
| break; | |
| } | |
| } | |
| std::cout << final_label << std::endl; | |
| } | |
| int main() { | |
| std::unordered_map<std::string, size_t> map; | |
| int pc = 17; | |
| map["main"] = 0; | |
| map["kek"] = 5; | |
| map["ok"] = 17; | |
| get_label(map, pc); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Took me almost an hour but I think it was worth it.