////////////////////////////////////////
// 2018/05/01 16:10:51
// lower_bound
// returns an iterator to the first element greater than a certain value
#include <iostream>
#include <map>
#include <ctime>
using namespace std;
unsigned long int gener_rand(){
unsigned long int random = (unsigned long int)(10000.0*rand() / (RAND_MAX + 1.0)) % 10;
return random;
}
//--------------------------
int main(){
unsigned long int ary[100];
typedef map<int, unsigned long int > M;
M m;
// initialize all values to 0
for (int i = 0; i < 100; i++){
ary[i] = 0;
}
srand(time(0));
// initialize ary[] with random values
for (int i = 0; i < 100; i++){
ary[i] = gener_rand();
}
for (int i = 0; i < 100; i++){
if (i % 10 == 0 && i != 0){
cout << endl;
}
cout << ary[i] << " ";
// generate freaquances
m[ary[i]] += 1;
}
cout << endl << endl;
M::iterator it = m.begin();
while (it != m.end()){
cout << "number " << it->first
<< " occured " << it->second
<< " time(s)." << endl;
it++;
}
cout << endl;
m[12] = 123;
m[15] = 234;
m[18] = 345;
// find leftmost node not less than _Keyval in mutable tree
it = m.lower_bound(11);
cout << "lower_bound(11) = " << it->first << endl;
// find leftmost node greater than _Keyval in mutable tree
it = m.upper_bound(11);
cout << "upper_bound(11) = " << it->first << endl;
return 0;
}
/*
OUTPUT:
3 2 3 4 3 4 0 3 7 6
4 1 2 4 5 1 9 2 2 9
4 1 3 4 2 5 7 5 3 9
0 1 0 2 8 1 4 2 4 4
8 1 0 6 4 4 6 0 0 0
5 6 9 1 4 0 0 8 8 1
8 5 4 3 9 3 3 1 6 9
9 2 8 0 0 3 7 4 3 8
9 6 3 9 1 0 3 7 4 4
1 4 6 8 8 5 6 1 3 6
number 0 occured 12 time(s).
number 1 occured 12 time(s).
number 2 occured 8 time(s).
number 3 occured 14 time(s).
number 4 occured 17 time(s).
number 5 occured 6 time(s).
number 6 occured 9 time(s).
number 7 occured 4 time(s).
number 8 occured 9 time(s).
number 9 occured 9 time(s).
lower_bound(11) = 12
upper_bound(11) = 12
*/