-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.1.7.1 Binary searching
60 lines (48 loc) · 1.06 KB
/
5.1.7.1 Binary searching
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <ctype.h>
#include <string>
using namespace std;
struct Country
{
string name;
int area;
Country(string n,int a):name(n),area(a){}
};
void print(const Country & v)
{
cout<<v.area<<" ";
}
void printMessage(bool value)
{
if (value)
{
cout<<"Found!\n";
}
else
{
cout<<"Not found!\n";
}
}
bool ascending_one_key(const Country& a,const Country& b)
{
return a.area < b.area;
}
int main()
{
vector <Country> countries = {
{"India", 3287 },{ "Argentina", 2780 },
{"Brazil", 8515}, {"Australia", 7692},
{"China", 9572 },{ "United States of America", 9525 },
{"Russia", 17098 },{ "Canada", 9984 },
{"Kazakhstan", 2724 }, {"Algeria", 2381 }
};
int area ;
cin >> area;
Country b("",area);
sort(countries.begin(), countries.end(),ascending_one_key);
printMessage(binary_search(countries.begin(), countries.end(), b,ascending_one_key));
return 0;
}