c++ - 尝试按任何类成员对对象 vector 进行排序

标签 c++ sorting vector

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

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

最佳答案

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

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

附言我喜欢在这些情况下使用的一个技巧是拥有一个可以根据参数按任一标准排序的仿函数类。

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

关于c++ - 尝试按任何类成员对对象 vector 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22925963/

相关文章:

c++ - clang 8.0 的编译器错误 -- 段错误 -- 在 Macos Sierra 10.12.3 上

c++打印包含结构的 vector

r - R中向量的输出类型

PHP:如何对字符串中的字符进行排序?

用于链接列表的 Mergesort 的 Python 实现不起作用

java - 如何按集合中对象的属性对列表进行排序?

用于多次搜索的 C++ 最快的数据结构

c++ - 在 C 中,我们可以使用诸如::或 -> 之类的运算符来访问头文件中的预定义方法吗?

c++ - Linux 中编译的程序未检测到 fstream 中的 '\n' 换行符,而 Windows 运行时会检测到

c++ - 读取包含 Unicode 字符的文件