Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created December 24, 2016 05:13
Show Gist options
  • Save hiroshi-maybe/6ace5227dbd64283ca8c059d3b619bc3 to your computer and use it in GitHub Desktop.
Save hiroshi-maybe/6ace5227dbd64283ca8c059d3b619bc3 to your computer and use it in GitHub Desktop.
Upper bound / lower bound
// http://cpp.sh
// Example program
#include <iostream>
#include <algorithm> // max,min
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
using namespace std;
int main()
{
int input[] = {1,3,5,5,7,10,45};
vector<int> v(input, input+7);
multiset<int> S;
for(auto &n: v) S.insert(n);
//cout << S.size() << endl;
vector<int> q = { 0,2,5,11,45,100};
for(auto &n: q) {
cout << n << endl;
auto it1 = S.lower_bound(n);
auto it2 = S.upper_bound(n);
bool ok1 = it1!=S.end(), ok2=it2!=S.end();
cout << "ok?: " << ok1 << "," << ok2 << endl;
cout << "l,u: " << *it1 << "," << *it2 << endl;
}
}
/**
Output:
0
ok?: 1,1
l,u: 1,1
2
ok?: 1,1
l,u: 3,3
5
ok?: 1,1
l,u: 5,7
11
ok?: 1,1
l,u: 45,45
45
ok?: 1,0
l,u: 45,7
100
ok?: 0,0
l,u: 7,7
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment