weather report functionaility


can anyone of you explain this line of code

set <W_report, less<W_report> > perset;

and the other statements like this in code below.....


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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include<iostream>
#include<queue>
#include<vector>
#include<climits>
#include<conio.h>
#include<string>
#include<string.h>
#include<set>
#include<algorithm>

using namespace std;

class W_report
{
private:
    string city;
    string report;


public:

    int distance;

    W_report()
    {
        city='\0';
        report='\0';
        distance=0;
    }

    W_report(string f, string l, int p) : city(f),report(l),distance(p)
    {

    }

    void display()
    {
        if(distance>0 )
            cout << " CITY : " << city << endl << " Weather Report : " << report << endl ;
    }

    void ldisplay() const
    {
    if(distance>0 )
        cout << " CITY : " << city << endl << " Weather Report : " << report << endl ;
    }

    friend bool operator < (const W_report &p1, const W_report& p);

    friend bool operator == (const W_report &p1, const W_report &p);
    bool searchcity (const W_report p) const
    {
        if(city==p.city)
            return true;
        return false;
    }
};


bool operator < (const W_report &p1,const W_report& p)
{
    if(p1.distance < p.distance)
        return true;

    return false;
}

bool operator == (const W_report &p1, const W_report &p)
{
    if((p1.distance==p.distance))
        return true;
    else
        return false;
}

void printreport(W_report w)
{
    w.display();
}

int main()
{
    W_report w1("Islamabad","It will Rain",200);
    W_report w2("Faisalabad","MAX temperature will be 32 degrees",300);
    W_report w3("Multan","MAX temperature will be 45 degrees",400);
    W_report w4("Peshawar","It could rain in the morning",500);
    W_report w5("Karachi","There will be no rain for 2 days",600);
    W_report w6("Hyderabad","It will Rain heavily for today",250);
    W_report w7("Rawalakot","There will be snowfall today!!! ",150);
    W_report w8("Lahore","No RAIN but MAX temperature will be 20 degrees only ",572);
    W_report w9("Bagh","COULD be snowfall today",700);
    W_report w10("Mirpur","MAX temp will exceed 50 degrees ",1000);
    W_report w11("Muzafarabad","There will be no rain but MAX temperature will be 24 degrees ",1400);
    W_report w12("Gilgit","MAX Temperature will be 40 degrees till evening, after that it could rain ",1600);
    W_report w13("Sialkot","In will rain for whole night ",1900);
    W_report w14("Swat","There could be rain ",1300);

    set <W_report, less<W_report> > perset;
    perset.insert(w1);
    perset.insert(w2);
    perset.insert(w3);
    perset.insert(w4);
    perset.insert(w5);
    perset.insert(w6);
    perset.insert(w7);
    perset.insert(w8);
    perset.insert(w9);
    perset.insert(w10);
    perset.insert(w11);
    perset.insert(w12);
    perset.insert(w13);
    perset.insert(w14);

    set <W_report, less<W_report> > :: iterator persit;

    persit=perset.begin();

    for_each(perset.begin(),perset.end(),printreport );

    W_report search_city("Swat","XYZ",380);

    for(int i=0;persit != perset.end();persit++,i++)
    {

        if((*persit).searchcity(search_city))
        {
            (*persit).ldisplay();
            break;
        }

    }

    int ldistance=10000;
    set<W_report, less<W_report> > :: iterator nearest_report;


    if(persit == perset.end() )
    {
        cout << " CITY REPORT NOT FOUND IN THE GIVEN DATABASE : RETURNING NEAREST REPORT " << endl;
        persit=perset.begin();
        nearest_report=persit;
        for(int i=0;persit != perset.end();persit++,i++)
        {
            if(ldistance<abs((*persit).distance-search_city.distance))
            {
                (*nearest_report).ldisplay();
                    break;
            }
            else
            {
                ldistance=abs((*persit).distance-search_city.distance);
                nearest_report=persit;
            }
        }
    }

    getch();
    return 0;
}



it is the implementation for the std::set container, containing W_report objects, ordered by the bool operator < (const W_report &p1,const W_report& p) function. In fact, the second part is redundant. See http://www.cplusplus.com/reference/set/set/ and http://www.cplusplus.com/reference/functional/less/
Topic archived. No new replies allowed.