簡體   English   中英

為什么在鍵類型中使用引用時 lambda 函數在 std::lower_bound 中不起作用?

[英]Why lambda function doesn't work in std::lower_bound while using references in key type?

我寫這個代碼:

std::vector<std::pair<std::string, std::string>> map_ = {{"b", "1"}, {"a", "2"}};
auto iter = std::lower_bound(map_.begin(), map_.end(), key,
                                     [](auto p1, std::string& rhs) { return p1.first < rhs; });

並得到這樣的編譯錯誤:

error: no matching function for call to object of type '(lambda at /Users/bestasoff/.../static_map.h:17:38)'
        if (__comp(*__m, __value_))

但是如果我刪除&std::lower_bound工作正常。

為什么?

如果您查看std::lower_bound謂詞的要求,您會發現它應該如下所示

bool pred(const Type1 &a, const Type2 &b);

所以你不能使用參考。 僅使用const限定符(或按值傳遞鍵)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM