我正在为我的妻子编写一个程序,以帮助我们尝试决定她应该申请哪些医学院。但是,我遇到了一个问题,我试图提取在 4 种不同情况下出现在前 20 名结果中的学校。
例如,在其中一个案例中,我将城市的收入中位数除以城市的平均房价。这会返回一个 double ,然后我创建一个新 vector ,然后根据该数字从最高到最低对该 vector 进行排序。我对池中的其他 3 个 vector 执行类似的操作,并应用不同的案例。
我知道我可以强制执行此操作并使用嵌套的 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:
对于那些试图编译这个程序的人,我深表歉意。它现在可以编译并运行。
最佳答案
当您执行诸如排序或 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 编写代码一样打算添加一些额外的工作(并且可能没有足够的关注)所以我没有打扰。我还省略了按学费筛选/分类——两者几乎相同。
关于c++ - 如何有效地比较相似名称的多个结构 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51429298/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack