我正在努力优化对具有“几乎”排序数据的数据结构的查找。我相当有信心它的“几乎”细节实际上并不重要,但我不确定
实际的数据结构比 SO 所需的更复杂,所以我对其进行了简化。简化版是std::vector<Level>
有价格、买价和卖价:
- 价格严格上升
- 出价通常按升序排列
- 提问一般按降序排列
当我说一般时,我的意思是数据有一长串通常为零的序列,后面跟着有意义的值,但有些零实际上可能是负数。但是,我只会搜索正值,因此所有零和负值都不是有意义的返回值
下面是我的SO简化程序的测试数据:
// Price Bid Ask Index
levels.emplace_back(Level( 42.0, 0, 150)); // 0
levels.emplace_back(Level( 43.0, 0, 71)); // 1
levels.emplace_back(Level( 44.0, 0, 70)); // 2
levels.emplace_back(Level( 45.0, 0, 70)); // 3
levels.emplace_back(Level( 46.0, 0, 69)); // 4
levels.emplace_back(Level( 47.0, 0, 0)); // 5
levels.emplace_back(Level( 48.0, -1, -1)); // 6
levels.emplace_back(Level( 49.0, 0, 0)); // 7
levels.emplace_back(Level( 50.0, 80, 0)); // 8
levels.emplace_back(Level( 51.0, 81, 0)); // 9
levels.emplace_back(Level( 52.0, 81, 0)); // 10
levels.emplace_back(Level( 53.0, 82, 0)); // 11
levels.emplace_back(Level( 54.0, 201, 0)); // 12
当我在这个结构中搜索一些出价“Seek Bid”时,我想找到出价大于或等于“Seek Bid”的第一个级别的价格
当我在这个结构中搜索一些 Ask,“Seek Ask”时,我想找到 Ask 大于或等于“Seek Ask”的最后一个 Level 的价格
下面是我针对 SO 的简化程序:
#include <algorithm>
#include <iostream>
#include <vector>
struct Level final {
Level() = delete;
Level(const double a_price, const int a_bid, const int a_ask) :
m_price(a_price),
m_bid (a_bid),
m_ask (a_ask)
{}
const double m_price;
const int m_bid;
const int m_ask;
};
int main(int argc, char** argv) {
if (argc != 3) {
std::cout << "Usage: " << argv[0] << " <Seek Bid> <Seek Ask>\n";
exit(1);
}
std::vector<Level> levels;
// Price Bid Ask Index
levels.emplace_back(Level( 42.0, 0, 150)); // 0
levels.emplace_back(Level( 43.0, 0, 71)); // 1
levels.emplace_back(Level( 44.0, 0, 70)); // 2
levels.emplace_back(Level( 45.0, 0, 70)); // 3
levels.emplace_back(Level( 46.0, 0, 69)); // 4
levels.emplace_back(Level( 47.0, 0, 0)); // 5
levels.emplace_back(Level( 48.0, -1, -1)); // 6
levels.emplace_back(Level( 49.0, 0, 0)); // 7
levels.emplace_back(Level( 50.0, 80, 0)); // 8
levels.emplace_back(Level( 51.0, 81, 0)); // 9
levels.emplace_back(Level( 52.0, 81, 0)); // 10
levels.emplace_back(Level( 53.0, 82, 0)); // 11
levels.emplace_back(Level( 54.0, 201, 0)); // 12
const int seekBid = atoi(argv[1]);
const int seekAsk = atoi(argv[2]);
std::cout << "Seek Bid: " << seekBid << ", Seek Ask: " << seekAsk << '\n';
if (seekBid <= 0 || seekAsk <= 0) {
std::cout << "Seek Bid or Seek Ask is not positive\n";
exit(1);
}
// If the last Level's Bid is < Seek Bid then what I am looking for doesn't exist
if (levels.back().m_bid < seekBid)
std::cout << "Cannot satisfy Seek Bid\n";
else {
// Find the first Level with a Bid <= Seek Bid
// Not sure why I need to specify < instead of <= but appears to work
const auto it = std::lower_bound(
levels.begin(),
levels.end(),
seekBid,
[](const Level& a_level, const int a_bid) { return a_level.m_bid < a_bid; }
);
std::cout << "Bid Price: " << it->m_price << ", Bid Index: " << &*it - &levels[0] << '\n';
}
// If the first Level's Ask is < Seek Ask then what I am looking for doesn't exist
if (levels.front().m_ask < seekAsk)
std::cout << "Cannot satisfy Seek Ask\n";
else {
// Find the last Level with Ask <= Seek Ask
// Need to use std::prev due to how std::upper_bound works
// Not sure why I need to specify < instead of <= but appears to work
const auto it = std::prev(std::upper_bound(
levels.begin(),
levels.end(),
seekAsk,
[](const int a_ask, const Level& a_level) { return a_level.m_ask < a_ask; }
));
std::cout << "Ask Price: " << it->m_price << ", Ask Index: " << &*it - &levels[0] << '\n';
}
return 0;
}
下面是一些为 SO 运行我的测试程序的例子。 “Seek Bid”为 81 而“Seek Ask”为 70 的情况非常重要,因为有两个 81 的出价和两个 70 的要价。在真实程序中重要的是找到前 81 个 Bid 和最后 70 个 Ask:
Seek Bid: 79, Seek Ask: 68
Bid Price: 50, Bid Index: 8
Ask Price: 46, Ask Index: 4
Seek Bid: 80, Seek Ask: 69
Bid Price: 50, Bid Index: 8
Ask Price: 46, Ask Index: 4
Seek Bid: 81, Seek Ask: 70
Bid Price: 51, Bid Index: 9
Ask Price: 45, Ask Index: 3
Seek Bid: 82, Seek Ask: 71
Bid Price: 53, Bid Index: 11
Ask Price: 43, Ask Index: 1
所有这些结果都是正确的,但这些是我的问题:
- 我有必要把所有的负数都变成零吗
在搜索之前保证正确的结果在使用之前
std::lower_bound
或std::upper_bound
考虑到我只是 寻找正值?换句话说,做消极的 根据我的搜索要求导致任何类型的未定义行为? - 如何描述
std::lower_bound
工作于 en.cppreference.com 和 cplusplus.com 非常困惑,我只 意识到使用<
而不是<=
在我的 lambdas 中是“正确的” 通过反复试验。为什么使用<=
不“正确”如果我是 寻找第一个/最后一个级别<=
我在寻找什么 为了?
最佳答案
Compare 中描述了一般要求.使用提供的比较,必须有一个单一的排序,以便等效元素组在该顺序中具有特定的位置。 lower_bound
和 upper_bound
要求输入按照这样的顺序。
Is it necessary for me to make all of the negative ones into zeroes before searching to guarantee correct results.
在这种特殊情况下不会,因为它只会测试 Level
s 与给定的正值相对,而不是相互对立。你的comp
对待 0
相当于-1
,因此它们“无序”并不重要。搜索 0
是未定义的行为或此数据集中的负数。
Why is it not "correct" to use
<=
if I am looking for the first / last Level that is<=
what I am searching for?
因为这打破了严格弱序的不对称要求。如果您只想要更大的值,请使用 upper_bound
.
关于c++ - 关于 std::lower_bound 和 std::upper_bound 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56205818/