尝试按任何类成员对对象矢量进行排序

时间:2014-04-08 01:22:30

标签: c++ sorting vector

我想弄清楚如何对City对象的矢量进行排序。每个对象都有一个cityName成员和一个cityCode成员。我希望访问器功能能够自动对矢量进行排序,然后以正确的排序顺序将其显示在屏幕上。我想我已经接近了,但有人可以告诉我我做错了什么吗?非常感谢。

//specification file for the 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 for the 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 for the 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 for the 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);
    for (City &c : cities)
        cout << "\t\t\t    " << c.getCityName() << "\t" << c.getCityCode() << endl;
}

void CityList::getCitiesByCode()
{
    sort (cities.begin(), cities.end(), sortByCityCode);
    for (City &c : cities)
        cout << "\t\t\t    " << c.getCityCode() << "\t\t" << c.getCityName() << endl;
}

1 个答案:

答案 0 :(得分:2)

您的sort来电正在使用SortByCityNameSortByCityCode,但这些是成员函数而不是独立函数。 sort调用不会将这些调用作为对象实例上的成员函数,因此成员函数与签名不匹配,无法找到它们。您可以通过两种不同的方式解决这个问题:您可以将它们完全从类中取出,或者您可以使它们成为static成员并使用类说明符,例如。

sort (cities.begin(), cities.end(), City::SortByCityName);

P.S。我喜欢在这些情况下使用的一个技巧是使用一个函数类,可以根据参数对任一条件进行排序。

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;
};