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
|
#include<set>
#include<iostream>
#include <string>
using namespace std;
class City
{
public:
City( float f1,
float f2,
string thecity);
string getCityName() ;
float getRainOne();
float getRainTwo() ;
private:
float rainOne;
float rainTwo;
string cityName;
};
City::City( float f1,
float f2,
string thecity
) : rainOne(f1), rainTwo(f2), cityName(thecity)
{
// for(int i=0;i<50;++i);
}
string City::getCityName()
{
return cityName;
}
float City::getRainOne()
{
return rainOne;
}
float City::getRainTwo()
{
return rainTwo;
}
template< class D >
struct comp
{
bool operator()( const D& c1, const D& c2) const
{
return ( c1->getCityName().length() > c2->getCityName().length() );
};
};
int main()
{
set<City*, comp<City*> > setContainer;
typedef set<City*, comp<City*> >::const_iterator ItCpp;
City* c1 = new City( 2.2, 2.3, "Piramboia");
City* c4 = new City( 3.3, 6.0, "Jarasdasdasdasdasdasdasdasdasd");
City* c2 = new City( 2.4, 2.3, "Carapicuiba");
City* c3 = new City( 2.4, 2.3, "Pindamongaba");
setContainer.insert( c1 );
setContainer.insert( c2 );
setContainer.insert( c3 );
setContainer.insert( c4 );
//for( auto it = setContainer.begin(); it != setContainer.end(); it++)// C++11
for( ItCpp it = setContainer.begin(); it != setContainer.end(); it++)// C++03
{
City* ptr = *it;
cout << "City: " << ptr->getCityName() << endl;
}
return 0;
}
|