Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- //#include <vector>
- #include <set>
- //#include <cmath>
- using namespace std;
- set<int>::const_iterator FindNearestElement(const set<int>& numbers, int border)
- {
- auto it1 = lower_bound(numbers.begin(), numbers.end(), border);
- auto it2 = it1;
- if (*it1==border) return it1;
- if (it1!=numbers.begin())
- {
- --it2;
- if (abs(-*it2+border)<=abs(-*it1+border)) return it2;
- } /*else*/ return it1;
- }
- int main()
- {
- setlocale(LC_ALL, "RU");
- set<int> numbers = { 1, 4, 6};
- cout << *FindNearestElement(numbers, 0) << " " << *FindNearestElement(numbers, 3) << " "
- << *FindNearestElement(numbers, 5) << " " << *FindNearestElement(numbers, 6) << " "
- << *FindNearestElement(numbers, 100) << endl;
- set<int> empty_set;
- cout << (FindNearestElement(empty_set, 8) == end(empty_set)) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement