如何有效比较相似名称的结构的多个向量?

时间:2018-07-19 18:08:37

标签: c++ algorithm

我正在为我的妻子编写一个程序,以帮助我们尝试确定她应申请的医学院。但是,我遇到了一个问题,我试图提取在4种不同情况下出现在前20名成绩中的学校。

例如,在其中一种情况下,我将城市的中位数收入除以城市的平均房价。返回一个双精度数,然后创建一个新向量,然后根据该数字从最高到最低对该向量进行排序。而且我对池中的其他3个媒介进行了类似的操作,并应用了不同的情况。

我知道我可以蛮力地用嵌套的for循环提取名称,但是我很好奇是否有办法快速有效地做到这一点。到目前为止,这是我的尝试。 (请注意,这只是一个示例,我的实际代码中有30所学校。)

#include <algorithm>
#include <iterator>
#include <iostream>
#include <memory>
#include <string>
#include <vector>

struct Schools
{
    Schools(std::string n = "", double h = 0.0, double t = 0.0, int r = 0, int w = 0) : name(n), housing(h), tuition(t), rank(r), weight(w){};
    std::string name;
    double housing;
    double tuition;
    int rank;
    int weight;
};

void load(std::vector<std::shared_ptr<Schools>> &v, std::string n, double h, double t, int r, int w)
{
    auto newSchool = std::make_shared<Schools>(n,h,t,r,w);
    v.emplace_back(newSchool);
}

void init(std::vector<std::shared_ptr<Schools>> &schools)
{
    load(schools,"School1",40.3,20.0,3,6);
    load(schools,"School2",10.3,10.4,5,1);
    load(schools,"School3",33.3,23.5,1,2);
    load(schools,"School4",8.5,15.5,4,8);
}

auto findIntersection(auto &a, auto &b)
{
    std::vector<std::shared_ptr<Schools>> in;
    std::set_intersection(begin(a),end(a),begin(b),end(b),std::back_inserter(in));
    return in;
}

auto findCommon(auto &housing, auto &tuition, auto &rank, auto &weight)
{
    std::vector<std::shared_ptr<Schools>> inCommon;
    inCommon = findIntersection(housing,tuition);
    inCommon = findIntersection(inCommon,rank);
    inCommon = findIntersection(inCommon,weight);
    return inCommon;
}

bool compareHM(const std::shared_ptr<Schools> &a, const std::shared_ptr<Schools> &b)
{
    return a->housing < b->housing;
}

bool compareT(const std::shared_ptr<Schools> &a, const std::shared_ptr<Schools> &b)
{
    return a->tuition < b->tuition;
}

bool compareRank(const std::shared_ptr<Schools> &a, const std::shared_ptr<Schools> &b)
{
    return a->rank > b->rank;
}

bool compareWeight(const std::shared_ptr<Schools> &a, const std::shared_ptr<Schools> &b)
{
    return a->weight > b->weight;
}



int main()
{
    std::vector<std::shared_ptr<Schools>> schools;
    init(schools);
    std::vector<std::shared_ptr<Schools>> sortByHousing = schools;
    std::vector<std::shared_ptr<Schools>> sortByTuition = schools;
    std::vector<std::shared_ptr<Schools>> sortByRank = schools;
    std::vector<std::shared_ptr<Schools>> sortByWeight = schools;

    std::sort(begin(sortByHousing),end(sortByHousing), compareHM);
    std::sort(begin(sortByTuition),end(sortByTuition), compareT);
    std::sort(begin(sortByRank),end(sortByRank), compareRank);
    std::sort(begin(sortByWeight),end(sortByWeight), compareWeight);

    std::vector<std::shared_ptr<Schools>> commonSchools = findCommon(sortByHousing,sortByTuition,sortByRank,sortByWeight);

    for (auto && e: commonSchools)
    {
        std::cout << e->name << std::endl;
    }
}

当我尝试使用std::set_intersection时遇到问题,然后我很快意识到我无法做类似begin(a)->name的事情。同样,我尝试提取在我所遇到的4种情况中都出现的通用名称。我该如何实施呢?我对std::set_intersection的想法是否太遥远?

谢谢!

编辑: 这是我的functionThatCompares

的一个示例
bool compareTuition(const std::shared_ptr<Schools> &a, const std::shared_ptr<Schools> &b)
{
    return a->tuition < b->tuition;
}

编辑2:示例输出

The top 20 sorted by Median/House are:
Name of Institution                               Median/House Price  Tuition Over 8 Years     Has Space Industry? Score

University of Alabama                             0.463577            0.722279                 1                   0.641825
University of Maryland                            0.38124             0.722617                 1                   0.527583
Johns Hopkins Univerty School of Medicine         0.38124             0.606103                 1                   0.629002
Indiana University                                0.335939            0.501944                 0                   0.669276
Ohio State University                             0.32499             0.610704                 1                   0.532156
Perelman School of Medicine                       0.26908             0.653143                 1                   0.411977
Duke University School of Medicine                0.246991            0.66683                  1                   0.370395
University of Wisconsin                           0.226581            0.64686                  0                   0.350278
Chicago Medical School                            0.221883            0.648157                 0                   0.342329
Northwestern University                           0.221883            0.677341                 0                   0.32758
Case Western Reserve                              0.211817            0.536384                 1                   0.394898
Emory University                                  0.206169            0.576814                 1                   0.357427
Geisel School of Medicine                         0.205529            0.71526                  0                   0.287349
University of Massachusetts                       0.19562             0.64686                  1                   0.302414
Medical University of SC                          0.185816            0.354728                 0                   0.523827
University of North Carolina                      0.176684            0.6637                   0                   0.26621
University of Michigan Medical School             0.158465            0.637237                 1                   0.248675
Rutgers New Jersey Medical School                 0.140412            0.722115                 0                   0.194446
University of Utah                                0.140142            0.311285                 0                   0.450205
Georgetown University                             0.128883            0.604001                 1                   0.213382

The top 20 sorted by Tuition are:
Name of Institution                               Median/House Price  Tuition Over 8 Years     Has Space Industry? Score

University of Utah                                0.140142            0.311285                 0                   0.450205
Medical University of SC                          0.185816            0.354728                 0                   0.523827
University of California, LA                      0.07633             0.47547                  1                   0.160536
Indiana University                                0.335939            0.501944                 0                   0.669276
University of California, SD                      0.109214            0.531397                 1                   0.205523
Case Western Reserve                              0.211817            0.536384                 1                   0.394898
Emory University                                  0.206169            0.576814                 1                   0.357427
Icahn School of Medicine                          0.0822029           0.585946                 1                   0.140291
Georgetown University                             0.128883            0.604001                 1                   0.213382
Johns Hopkins Univerty School of Medicine         0.38124             0.606103                 1                   0.629002
Ohio State University                             0.32499             0.610704                 1                   0.532156
University of Virgina School of Medicine          0.123755            0.630067                 1                   0.196415
University of Michigan Medical School             0.158465            0.637237                 1                   0.248675
NY University School of Medicine                  0.0822029           0.642516                 1                   0.127939
Tufts University School of Medicine               0.100302            0.644314                 1                   0.155672
University of Massachusetts                       0.19562             0.64686                  1                   0.302414
University of Wisconsin                           0.226581            0.64686                  0                   0.350278
Chicago Medical School                            0.221883            0.648157                 0                   0.342329
Perelman School of Medicine                       0.26908             0.653143                 1                   0.411977
Standford University School of Medicine           0.0780054           0.656658                 1                   0.118791

The top 20 sorted by Median/House and Loweset Tution are:
Name of Institution                               Median/House Price  Tuition Over 8 Years     Has Space Industry? Score

University of Maryland                            0.38124             0.722617                 1                   0.527583
Chicago Medical School                            0.221883            0.648157                 0                   0.342329
University Of Washington                          0.0973689           0.761413                 1                   0.127879
University of Alabama                             0.463577            0.722279                 1                   0.641825
Rutgers New Jersey Medical School                 0.140412            0.722115                 0                   0.194446
Geisel School of Medicine                         0.205529            0.71526                  0                   0.287349
Ohio State University                             0.32499             0.610704                 1                   0.532156
Harvard Medical School                            0.100302            0.710787                 1                   0.141114
Duke University School of Medicine                0.246991            0.66683                  1                   0.370395
Boston University School of Medicine              0.100302            0.710787                 1                   0.141114
Perelman School of Medicine                       0.26908             0.653143                 1                   0.411977
University of Wisconsin                           0.226581            0.64686                  0                   0.350278
University of North Carolina                      0.176684            0.6637                   0                   0.26621
Standford University School of Medicine           0.0780054           0.656658                 1                   0.118791
Johns Hopkins Univerty School of Medicine         0.38124             0.606103                 1                   0.629002
Northwestern University                           0.221883            0.677341                 0                   0.32758
Indiana University                                0.335939            0.501944                 0                   0.669276
Case Western Reserve                              0.211817            0.536384                 1                   0.394898
Emory University                                  0.206169            0.576814                 1                   0.357427
University of Massachusetts                       0.19562             0.64686                  1                   0.302414

The top 20 sorted by Score (Median/House * Tuition/Salary) are:
Name of Institution                               Median/House Price  Tuition Over 8 Years     Has Space Industry? Score

Indiana University                                0.335939            0.501944                 0                   0.669276
University of Alabama                             0.463577            0.722279                 1                   0.641825
Johns Hopkins Univerty School of Medicine         0.38124             0.606103                 1                   0.629002
Ohio State University                             0.32499             0.610704                 1                   0.532156
University of Maryland                            0.38124             0.722617                 1                   0.527583
Medical University of SC                          0.185816            0.354728                 0                   0.523827
University of Utah                                0.140142            0.311285                 0                   0.450205
Perelman School of Medicine                       0.26908             0.653143                 1                   0.411977
Case Western Reserve                              0.211817            0.536384                 1                   0.394898
Duke University School of Medicine                0.246991            0.66683                  1                   0.370395
Emory University                                  0.206169            0.576814                 1                   0.357427
University of Wisconsin                           0.226581            0.64686                  0                   0.350278
Chicago Medical School                            0.221883            0.648157                 0                   0.342329
Northwestern University                           0.221883            0.677341                 0                   0.32758
University of Massachusetts                       0.19562             0.64686                  1                   0.302414
Geisel School of Medicine                         0.205529            0.71526                  0                   0.287349
University of North Carolina                      0.176684            0.6637                   0                   0.26621
University of Michigan Medical School             0.158465            0.637237                 1                   0.248675
Georgetown University                             0.128883            0.604001                 1                   0.213382
University of California, SD                      0.109214            0.531397                 1                   0.205523

编辑3:

对于那些尝试编译此程序的人,我深表歉意。现在,它可以编译并运行。

2 个答案:

答案 0 :(得分:2)

当您执行诸如sort或set_intersection之类的操作时,可以指定如何进行比较。如果您未指定任何内容,则将使用operator<作为类型(如果已定义)。

在这种情况下,您似乎想使用partial_sort_copy而不是sort。这样一来,您可以按每种排序(例如)获得排名前10的学校。

然后,您必须按名称对它们进行重新排序以进行set_intersection。

然后,您将执行set_intersection以获取这些集合之间的通用学校。

这里有一些演示代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <memory>

struct School
{
    std::string name;
    double housing;
    double tuition;
    int rank;
    int weight;

    // default comparison to use if nothing else is specified:
    bool operator<(School const &other) const { return name < other.name; }
};

int main()
{
    std::vector<School> schools{
        {"School1", 40.3, 20.0, 3, 6},
        {"School2", 10.3, 10.4, 5, 1},
        {"School3", 33.3, 23.5, 1, 2},
        {"School4", 8.5, 15.5, 4, 8}
    };

    // We specify the size of each of these as 3, so when we do the
    // partial_sort_copy, it'll fill in the top 3 for that category.
    std::vector<School> byHousing(3);
    std::vector<School> byRank(3);
    std::vector<School> byWeight(3);

    std::partial_sort_copy(schools.begin(), schools.end(), byHousing.begin(), byHousing.end(),
                           [](School const &a, School const &b) { return a.housing < b.housing; });

    std::partial_sort_copy(schools.begin(), schools.end(), byRank.begin(), byRank.end(),
                           [](School const &a, School const &b) { return a.rank < b.rank; });

    std::partial_sort_copy(schools.begin(), schools.end(), byWeight.begin(), byWeight.end(),
                           [](School const &a, School const &b) { return a.weight < b.weight; });

    std::sort(byHousing.begin(), byHousing.end());
    std::sort(byRank.begin(), byRank.end());
    std::sort(byWeight.begin(), byWeight.end());

    std::vector<School> temp, commonSchools;

    std::set_intersection(byHousing.begin(), byHousing.end(), byRank.begin(), byRank.end(),
                          std::back_inserter(temp));

    std::set_intersection(temp.begin(), temp.end(), byWeight.begin(), byWeight.end(),
                          std::back_inserter(commonSchools));

    std::cout << "Common Schools\n";
    for (auto const & e: commonSchools)
    {
        std::cout << e.name << "\n";
    }
}

结果:

Common Schools
School3

顺便说一句,在我看来,使用shared_ptr编写代码会增加一些额外的工作(可能没有足够的精力来关心),所以我没有打扰。我还省去了按学费进行的筛选/分类-几乎一样。

答案 1 :(得分:1)

std::set_intersection文档中所述

  

3)使用给定的二进制比较函数comp对元素进行比较,并且必须对范围进行排序

(强调是我的),因此您不能立即将其与向量一起使用,需要在应用name之前将它们全部遵循一个通用准则,例如使用std::set_intersection。为std::set_intersection本身提供相同的比较器

我将为每所学校创建一个权重,并使其根据每个向量的指数成指数增长,然后将它们求和并基于该值进行排序。获胜最少的人。您还可以在排序决策中添加用于增加/减少某些条件的权重的系数:

std::unordered_map<std::shared_ptr<Schools>,double> weights;
size_t count = 0;
auto weightCalc = [&weights, &count]( std::shared_ptr<Schools> s ) {
    weights[s] += std::exp( count++ );
};
std::for_each( sortByHousing.begin(), sortByHousing.end(), weightCalc );
count = 0;
std::for_each( sortByTuition.begin(), sortByTuition.end(), weightCalc );
count = 0;
std::for_each( sortByRank.begin(), sortByRank.end(), weightCalc );
count = 0;
std::for_each( sortByWeight.begin(), sortByWeight.end(), weightCalc );

std::multimap<double,std::shared_ptr<Schools>> sortedSchools;
std::copy( weights.begin(), weights.end(), std::inserter( sortedSchools ), []( const auto &p ) { return std::make_pair( p.second, p.first ); } );

然后使用sortedSchools的前n所学校