4

我需要获取 a 中的最小元素std::map。我知道有很多可用的文档;但是,我似乎无法工作。

我有两个地图,bidask,它们都是Book该类的属性。每个都是队列地图。这些队列中的每一个都包含Order对象(具有各种属性,如pricevolume等)。我有一个成员函数update,它可以获得最佳出价、最佳要价和点差:

void update(void)
{
  unsigned long long highest_bid, lowest_ask = 0;

  for (std::map<unsigned long long, queue<Order>>::iterator it = this->bid.begin(); it != this->bid.end(); ++it)
  { 
    highest_bid = it->first;
  }

  // best ask code here

  this->bestBid = highest_bid;
  this->bestAsk = lowest_ask;
  this->spread = labs(this->bestAsk - this->bestBid);
}

询问代码在哪里,我尝试了以下方法:

lowest_ask = this->ask.begin()->first;

这可以编译,但是当我调试时它会引发断言失败(我在这里阅读了其他问题,但似乎无法理解):

Expression: map/set iterator not dereferencable

我试过反向迭代:

for(std::map<unsigned long long, queue<Order>>::reverse_iterator rit = this->ask.rbegin(); rit != this->ask.rend(); ++rit)
{
  lowest_ask = rit->first;
}

哪个编译和调试很好,但lowest_ask始终为 0,这是错误的。当我在调试器中单步执行它时,它不会停止,直到它达到零。

我试过交换迭代器:

for(std::map<unsigned long long, queue<Order>>::reverse_iterator rit = this->ask.rend(); rit != this->ask.rbegin(); ++rit)
{
  lowest_ask = rit->first;
}

这编译得很好,但又一次抛出了调试断言失败。

我可以继续我已经尝试过的内容,但是这个问题已经过于复杂了。我只是不明白为什么我不能做我一开始所做的事情(lowest_ask = this->ask.begin()->first)。

非常感谢您提前。

4

3 回答 3

10

遍历地图并始终分配相同的变量似乎是不必要的艰苦工作。

如果您需要访问地图中的第一项(或地图中的最后一项),那么您只需要 begin()(或 rbegin())即可。

    std::map <int, int> themap;

    themap[4] = 1;
    themap[2] = 2;
    themap[1] = 3;
    themap[6] = 4;
    themap[5] = 5;
    themap[7] = 6;

    if (!themap.empty())
    {
        std::cout << "item[" << themap.begin()->first << "] = " << themap.begin()->second << std::endl;
        std::cout << "item[" << themap.rbegin()->first << "] = " << themap.rbegin()->second << std::endl;
    }

唯一需要小心 begin 和 rbegin 的时候是你的地图是空的

于 2014-07-19T14:16:42.930 回答
2

我认为您可能只需要检查您的容器是否为空,begin()然后rbegin()返回一些有意义的(定义的)

尝试这个:

void update(void)
{
    if(bid.empty() || ask.empty())
        return;

    // best ask code here

    this->bestBid = bid.rbegin()->first;
    this->bestAsk = ask.begin()->first;
    this->spread = labs(this->bestAsk - this->bestBid);
}
于 2014-07-19T14:16:37.960 回答
-1

这并不“复杂”;它只需要一些标准的调试措施

#include <map>
#include <iostream>
#include <algorithm>
#include <random>
#include <string>
#include <queue>


namespace mock {
    using Order = std::string;


    struct Book {
        using key_type = unsigned long long;  
        using order_queue_type = std::queue<Order>;        
        using property_type = std::map<key_type, order_queue_type>; 

        property_type bids, asks;


        void diagnose(const property_type& prop) {
            for (auto it = prop.cbegin(); it != prop.cend(); ++it) {
                std::clog << "\t" << it->first << '\n';
            }
        }

        void diagnose() { 
            std::clog << "bids:" << '\n';     
            diagnose(bids);
            std::clog << "asks:" << '\n';     
            diagnose(asks);
        } 

        Book() {
            std::random_device rd;
            std::mt19937 gen(rd());
            std::uniform_int_distribution<key_type> ba_dist(0, 1000);
            std::uniform_int_distribution<std::size_t> len_dist(0, 10);

            auto prop_gen = [&] (property_type& prop) {
                auto count = len_dist(gen);
                for (std::size_t i = 0; i < count; ++i) {
                    auto val = ba_dist(gen);
                    auto pair = prop.emplace(val, order_queue_type());
                    if (!pair.second) {
                        std::clog << val << " already present" << '\n';
                    }
                }
            };

            prop_gen(bids);
            prop_gen(asks);
        }
    };
}


int main() {
    mock::Book book; 
    book.diagnose();
}    

当然,不要使用我的Bookctor 中的生成器,而是使用您的 init 例程和您的Order类型。

于 2014-07-20T01:51:19.253 回答