- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对象数组 tArray 包含买家姓名和购买数量,每个买家可以多次出现在对象数组中。我必须以数组的形式返回五个最大买家的名字。
我尝试并行运行两个数组,其中包含买家名称,并且在另一个数组中有总体积。
我的方法总体上有缺陷,因为我得到了错误的结果,我该如何解决这个问题。
谢谢
ntransactions = 数组中的交易数
string* Analyser::topFiveBuyers()
{
//set size and add buyer names for comparison.
const int sSize = 5;
string *calcString = new string[sSize];
calcString[0] = tArray[0].buyerName;
calcString[1] = tArray[1].buyerName;
calcString[2] = tArray[2].buyerName;
calcString[3] = tArray[3].buyerName;
calcString[4] = tArray[4].buyerName;
int calcTotal[sSize] = {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN};
//checks transactions
for (int i = 0; i<nTransactions; i++)
{
//compares with arrays
for(int j =0; j<sSize; j++)
{
//checks if the same buyer and then increase his total
if(tArray[i].buyerName == calcString[j])
{
calcTotal[j] += tArray[i].numShares;
break;
}
//checks if shares is great then current total then replaces
if(tArray[i].numShares > calcTotal[j])
{
calcTotal[j] = tArray[i].numShares;
calcString[j] = tArray[i].buyerName;
break;
}
}
}
return calcString;
}
最佳答案
假设您被允许,我会先将这些值累加到一个 std::map 中:
std::map<std::string, int> totals;
for (int i=0; i<ntransactions; i++)
totals[tarray[i].buyername] += tarray[i].numshares;
这会将每位买家的股份总数相加。然后您想将该数据复制到 std::vector,并按份额数获得前 5 名。目前,我假设您的结构(以 buyername
和 numshares
作为成员)被命名为 transaction
。
std::vector<transaction> top5;
std::copy(totals.begin(), totals.end(), std::back_inserter(top5));
std::nth_element(top5.begin(), top5.begin()+5, top5.end(), by_shares());
为此,您需要一个名为 by_shares
的比较仿函数,它类似于:
struct by_shares {
bool operator()(transaction const &a, transaction const &b) {
return b.numshares < a.numshares;
}
};
或者,如果您使用的编译器足够新以支持它,您可以使用 lambda 而不是显式仿函数来进行比较:
std::nth_element(totals.begin(), totals.end()-5, totals.end(),
[](transaction const &a, transaction const &b) {
return b.numshares < a.numshares;
});
无论哪种方式,在第 nth_element 完成后,您的前 5 个元素将位于 vector 的前 5 个元素中。我已经颠倒了正常的比较来做到这一点,所以它基本上是按降序进行的。或者,您可以使用升序,但指定从集合末尾开始的第 5 个位置,而不是从开头开始的第 5 个位置。
我应该补充一点,还有其他方法可以做到这一点——例如,Boost 双映射也能很好地完成这项工作。鉴于这听起来像是家庭作业,我的猜测是像 bimap 这样可以为您处理几乎整个工作的预打包解决方案可能不会/不会被允许(甚至 std::map
可能出于几乎相同的原因被禁止)。
关于c++ - 棘手的方法 - 需要解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15362486/
我有以下两个表: T1(身份证,名字)T2 (id,hybrid_col) 我想做的是从 T2 中选择所有内容,如果 hybrid_col 是数字,则使用 T1 加入。基本上,hybrid_col 持
这是代码:https://play.golang.org/p/Sizbc3uJt_c 我尝试替换这个简单的循环 for c := n.FirstChild; c != nil; c = c.NextS
我在大学参加了微软编码挑战,提出的问题是: Write a program that takes two strings as input, one is a query, and the other
我需要比较以下函数的增长率: f(n)=2^n 和 g(n)=n^log(n)(当 n 接近正无穷大时)。 这可能吗? 最佳答案 令 n = 2^k。我们有: 2^n = 2^(2^k) n^log(
我的服务器快满了,我需要自动删除文件。文件通常每天都会添加到我的服务器,但有时会有暂停,使它们每两周或每月一次。他们停止进来几个月然后又开始了,这是不可预测的。 我的脚本需要删除超过 30 天的文件但
我无法获得适用于 SonarQube 4.0 的代理配置,以便我可以安装插件。 当我打开 http://localhost:9000/updatecenter/available它显示错误:“未连接到
标题不是很清楚,但很难描述我遇到的问题。 让我们考虑一个实现了 == 和 != 方法的 Signal 类。 (这是我的简化版)。 import numpy as np class Signal:
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我想将焦点设置到我网站上的文本字段。有两个问题: 文本字段位于外部页面(我无法控制)。它使用 iframe 嵌入到我的页面中。 每次加载页面时,文本字段都会生成一个自己的随机 ID。 这是我想要聚焦的
我有一个非常棘手的问题,我现在正试图弄清楚它, 我有这个查询结果集 SELECT * FROM Orders OrderID | OrderAmount | OrderDate | E
当我启动 webkit 浏览器实例并输入 http://localhost 时,$this.innerWidth()的结果是对的(我用的是jQuery)。 但是如果我尝试刷新页面, $this.inn
首先我知道有很多人问过这个问题!但我还有一个问题。 我正在做的是,通过 phpmyadmin 将我的数据库 (MySql) 导出到 .sql 文件。没问题。当我尝试将其导入“SQLite Databa
我这里有一个棘手的问题..请帮助.. 我有一个名为“DemoViewController”的 ViewController,两个不同的 Xib(Demo1Controller.xib 和 Demo2C
哦,嗨。我是一名初级 Java 开发人员,在空闲时间从事一些基于 2D 图 block 的游戏。现在我正在尝试实现游戏模型中非常基本的东西 - 各种类型的对象如何彼此交互。我希望有一天添加网络支持,所
假设我有以下两个表: PRICE price_id price room_id nr_of_people 1 80 1
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在开发一个 jQuery 插件,人们可以将其包含在自己的页面中。该插件在我正在操作的位于不同域的服务中生成作业。 为了突破域边界,我使用 jQuery 的 JSONP 功能,它可以很好地生成作业。
我看到很多开发人员只是盲目地按照分步说明将 JAX-WS RI jar 复制到 Tomcat 认可文件夹。也没有看到有人问为什么。 1) 如果 JDK 6 update 4+ 已经包含 JAX-WS
我找不到解决这个问题的方法。 这是我想要的渲染图: http://jsfiddle.net/kQSxb/ HTML: Lorem ipsum dolor s
我是一名优秀的程序员,十分优秀!