c++ - Trying to sort a vector of objects by any class member -
i trying figure out how sort vector of city objects. each object has cityname member , citycode member. i'd accessor functions automatically sort vector display on screen in proper sorted order. think i'm close, can please tell me i'm doing wrong? much.
//specification file city class #ifndef city_h #define city_h #include <string> using namespace std; class city { protected: string cityname; string citycode; public: //constructor city(); city(string name, string code); //setter void setcityname(string name); void setcitycode(string code); //getter string getcityname(); string getcitycode(); bool sortbycityname(const city & c1, const city & c2) { return c1.cityname < c2.cityname; } bool sortbycitycode(const city & c1, const city & c2) { return c1.citycode < c2.citycode; } }; #endif
//implementation file city class #include "city.h" //constructor city::city() { cityname = ""; citycode = ""; } city::city(string name, string code) { cityname = name; citycode = code; } //setter void city::setcityname(string name) { cityname = name; } void city::setcitycode(string code) { citycode = code; } //getter string city::getcityname() { return cityname; } string city::getcitycode() { return citycode; }
//specification file citylist class #ifndef citylist_h #define citylist_h #include <iostream> #include <string> #include <vector> #include <algorithm> #include "city.h" using namespace std; class citylist { private: vector<city> cities; public: //constructor citylist(); //setter void addcity(string name, string code); //getter void getcitiesbyname(); void getcitiesbycode(); friend bool city::sortbycityname(const city & c1, const city & c2); friend bool city::sortbycitycode(const city & c1, const city & c2); }; #endif
//implementation file citylist class #include "citylist.h" //constructor citylist::citylist() { cities.push_back(city("atlanta ", "ggg")); cities.push_back(city("orlando ", "fff")); cities.push_back(city("dallas/fort worth", "eee")); cities.push_back(city("new york city ", "ddd")); cities.push_back(city("hawaii ", "ccc")); cities.push_back(city("chicago ", "bbb")); cities.push_back(city("los angeles ", "aaa")); } //setter void citylist::addcity(string name, string code) { cities.push_back(city(name, code)); } //getter void citylist::getcitiesbyname() { sort (cities.begin(), cities.end(), sortbycityname); (city &c : cities) cout << "\t\t\t " << c.getcityname() << "\t" << c.getcitycode() << endl; } void citylist::getcitiesbycode() { sort (cities.begin(), cities.end(), sortbycitycode); (city &c : cities) cout << "\t\t\t " << c.getcitycode() << "\t\t" << c.getcityname() << endl; }
your sort
calls using sortbycityname
, sortbycitycode
, member functions not free-standing functions. sort
call doesn't call member functions on object instance, member functions don't match signature , can't find them. can fix in 2 different ways: can either take them out of class altogether, or can make them static
members , use class specifier e.g.
sort (cities.begin(), cities.end(), city::sortbycityname);
p.s. 1 trick use in these situations have single functor class can sort either criteria depending on parameter.
struct comparecities { comparecities(bool byname) : _byname(byname) {} bool operator()(const city & c1, const city & c2) { if (_byname) return c1.cityname < c2.cityname; else return c1.citycode < c2.citycode; } bool _byname; };
Comments
Post a Comment