[英]How do I compare multiple vector of structs for similar names efficiently?
我正在为我的妻子写一个程序来帮助我们决定她应该申请哪所医学院。 但是,我遇到了一个问题,我试图提取在4个不同案例中出现在前20名结果中的学校。
例如,在其中一种情况下,我采用城市的中位数收入,并将其除以城市的平均房价。 返回一个double,然后我创建一个新的向量,然后根据该数字从最高到最低对该向量进行排序。 我对我的池中的其他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:
我非常为那些试图编译这个程序的人道歉。 它现在编译并运行。
当您执行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
制作代码会增加一些额外的工作(可能没有足够的关注)所以我没有打扰。 我也省略了学费的筛选/分类 - 它几乎是相同的。
如std :: set_intersection的文档中所述
3)使用给定的二进制比较函数comp比较元素,并且范围必须相对于它们进行排序 。
(重点是我的)因此你不能立即将它与你的向量一起使用,你需要在使用std::set_intersection
给它们并向std::set_intersection
提供相同的比较器之前,使用一个通用标准,例如使用name
。 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所学校
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.